C# 随机数猜谜游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9086168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
random number guessing game
提问by
I am making a random number guessing game which the computer thinks of a number between 1-100. It then asks you what it is and tells you if you are right or wrong. However, whenever I debug, it says that it is higher or lower than the actual random number for some reason. Plus, it says two of those statements at once. Also, I'm not sure how to say how many guesses the person has taken. Here is my unsuccessful code.
我正在制作一个随机数猜谜游戏,计算机会认为它是 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 higher than " + Guess + " . Can you guess what it is?");
Console.ReadLine();
}
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();
}
}
采纳答案by OnResolve
You're using a lotof unneeded iteration. The while statement takes a boolean condition just like an IF statement.
您正在使用大量不必要的迭代。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();
}
回答by Mike Christensen
Try making the whiles ifs instead. Such as:
尝试使while小号if!而非。如:
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");
}
You also might want to put the:
您可能还想放置:
Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?");
prompt inside the whileloop so it will keep asking you before each prompt.
while循环内的提示,因此它会在每个提示之前不断询问您。
回答by Muad'Dib
you need to change your Whileloops to if-then-else statements
您需要将While循环更改为 if-then-else 语句
a while will run its code as long as the statement is true. so, in your code, you run the first one--basically forever because you are not resetting either of the values in your condition.
只要语句为真,一段时间就会运行它的代码。因此,在您的代码中,您运行第一个 - 基本上永远,因为您没有重置条件中的任何一个值。
if your while loop WERE to exit, then you have the same problem with the other while loops. you want something like this:
如果您的 while 循环要退出,那么其他 while 循环也会遇到同样的问题。你想要这样的东西:
if ( guess > myValue ) { // do something }
else ( guess < myValue ) {//do something else}
else { // do a third thing }
回答by Trevor Elliott
Try to restructure the logic so it does exactly what you want.
尝试重构逻辑,使其完全符合您的要求。
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!");
}
}
回答by John
As others have said, you are misusing whilewhere ifis really neeeded.
正如其他人所说,你是滥用while那里if真的neeeded。
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.");
}
回答by Nope Nope
Dude...
伙计...
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);
回答by carl manicar
Generates a random number between 1 and 9 (including 1 and 9). Ask the user the user to get the number, then tell them whether they guessed too low or too high, or exactly right.Extras:keep the game going until the user type ‘exit' keep track of how many guesses the user has taken, and when the game ends, print this out.
生成 1 到 9(包括 1 和 9)之间的随机数。要求用户获取数字,然后告诉他们他们是否猜得太低或太高,或者完全正确。 附加:保持游戏进行直到用户键入“退出” 跟踪用户进行了多少次猜测,当游戏结束时,打印出来。

