随机猜数字游戏
我正在制作一个随机猜数游戏,计算机会想到 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();
}
}
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.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您使用了很多不需要的迭代。 while 语句采用布尔条件,就像 IF 语句一样。
You're using a lot of unneeded iteration. The while statement takes a Boolean condition just like an IF statement.
尝试重构逻辑,使其完全符合您的要求。
Try to restructure the logic so it does exactly what you want.
尝试使用
while
来代替if
。例如:您可能还想将:
提示符放入
while
循环中,这样它就会在每个提示符之前不断询问您。Try making the
while
sif
s instead. Such as:You also might want to put the:
prompt inside the
while
loop so it will keep asking you before each prompt.您需要将
While
循环更改为 if-then-else 语句,只要该语句为 true,while 就会运行其代码。
因此,在您的代码中,您运行第一个 - 基本上永远运行,因为您没有重置条件中的任何一个值。
如果您的 while 循环要退出,那么其他 while 循环也会遇到同样的问题。
你想要这样的东西:
you need to change your
While
loops to if-then-else statementsa 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:
正如其他人所说,您在真正需要
if
的地方滥用了while
。As others have said, you are misusing
while
whereif
is really neeeded.老兄...
Dude...
生成 1 到 9 之间的随机数(包括 1 和 9)。要求用户得到这个数字,然后告诉他们是否猜得太低或太高,或者完全正确。附加功能:让游戏继续进行,直到用户输入“退出”,跟踪用户进行了多少次猜测,并在游戏结束时打印出来。
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.
你好,也许这对你来说没问题,但对其他想尝试的人来说:
Hello maybe it's okay for you but for the other who want to try :