gpt4 book ai didi

c# - 随机数猜谜游戏

转载 作者:行者123 更新时间:2023-11-30 14:37:35 25 4
gpt4 key购买 nike

我正在制作一个随机数猜谜游戏,计算机会想到 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 语句像 IF 语句一样采用 bool 条件。

    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.ReadLine());

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();

}

关于c# - 随机数猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9086168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com