随机数猜谜游戏
我正在制作一个随机数猜测游戏,计算机认为是1-100之间的数字。 然后它会询问你它是什么,并告诉你你是对还是错。 但是,每当我调试时,由于某种原因它会说它高于或低于实际随机数。 此外,它同时说出了其中两个陈述。 另外,我不知道怎么说这个人有多少猜测。 这是我不成功的代码。
static void Main(string[] args) { Random random = new Random(); int returnValue = random.Next(1, 100); int Guess = 0; Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); while (Guess != returnValue) { Guess = Convert.ToInt32(Console.Read()); while (Guess returnValue) { Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); Console.ReadLine(); } } while (Guess == returnValue) { Console.WriteLine("Well done! The answer was " + returnValue); Console.ReadLine(); } }
你正在使用大量不必要的迭代。 while语句采用布尔条件,就像IF语句一样。
static void Main(string[] args) { Random random = new Random(); int returnValue = random.Next(1, 100); int Guess = 0; Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); while (Guess != returnValue) { Guess = Convert.ToInt32(Console.Read()); if (Guess < returnValue) { Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?"); } else if (Guess > returnValue) { Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?"); } } Console.WriteLine("Well done! The answer was " + returnValue); Console.ReadLine(); }
尝试重构逻辑,使其完全符合您的要求。
Random r = new Random(); int val = r.Next(1, 100); int guess = 0; bool correct = false; Console.WriteLine("I'm thinking of a number between 1 and 100."); while (!correct) { Console.Write("Guess: "); string input = Console.ReadLine(); if (!int.TryParse(input, out guess)) { Console.WriteLine("That's not a number."); continue; } if (guess < val) { Console.WriteLine("No, the number I'm thinking is higher than that number."); } else if (guess > val) { Console.WriteLine("No, the number I'm thinking is lower than that number."); } else { correct = true; Console.WriteLine("You guessed right!"); } }
if
是s,请尝试制作while
s。 如:
if (Guess < returnValue) { Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); } if (Guess > returnValue) { Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); }
您也可能想要:
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
在while
循环中提示while
以便在每次提示之前不断询问你。
你需要将While
循环更改为if-then-else语句
只要语句为真,一段时间就会运行它的代码。 所以,在你的代码中,你运行第一个 – 基本上是永远的,因为你没有重置条件中的任何一个值。
如果你的while循环WERE要退出,那么你在while循环中遇到同样的问题。 你想要这样的东西:
if ( guess > myValue ) { // do something } else ( guess < myValue ) {//do something else} else { // do a third thing }
正如其他人所说,你是在滥用, while
if
真的需要的话。
static void Main(string[] args) { Random random = new Random(); int returnValue = random.Next(1, 100); int Guess = 0; int numGuesses = 0; Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); while (Guess != returnValue) { Guess = Convert.ToInt32(Console.Read()); string line = Console.ReadLine(); // Get string from user if (!int.TryParse(line, out Guess)) // Try to parse the string as an integer Console.WriteLine("Not an integer!"); else { numGuesses++; if (Guess < returnValue) { Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); } if (Guess > returnValue) { Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); } } } Console.WriteLine("Well done! The answer was " + returnValue + ".\nYou took " + numGuesses + " guesses."); }
多德…
int total = 1, low = 0, high = 0; int ranNum1, guess; string guessStr; Random ranNumGen = new Random(); ranNum1 = ranNumGen.Next(1, 10); Console.Write("Enter your guess >> "); guessStr = Console.ReadLine(); guess = Convert.ToInt16(guessStr); while (guess != ranNum1 ) { while (guess < ranNum1) { Console.WriteLine("Your guess is to low, try again."); Console.Write("\nEnter your guess >> "); guessStr = Console.ReadLine(); guess = Convert.ToInt16(guessStr); ++total; ++low; } while (guess > ranNum1) { Console.WriteLine("Your guess is to high, try again."); Console.Write("\nEnter your guess >> "); guessStr = Console.ReadLine(); guess = Convert.ToInt16(guessStr); ++total; ++high; } } //total = low + high; Console.WriteLine("It took you {0} guesses to correctly guess {1}", total, ranNum1);
生成1到9之间的随机数(包括1和9)。 让用户询问用户是否获得该号码,然后告诉他们他们是否猜到太低或太高,或者说是完全正确。 附加function:保持游戏继续进行,直到用户输入’退出’跟踪用户已经进行了多少猜测,并且当游戏结束时,打印出来。