我正在制作一个随机数字猜测游戏,电脑会想一个介于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();
}
}
while
循环,这就是为什么会打印多个提示的原因。一旦找到匹配项,您可以跳出while
循环,但最好只是用if
语句替换内部的while
循环。 - Cody Gray