当某些条件发生时,如何在while循环中保持变量的随机化值恒定
本文关键字:循环 变量 随机化 while 条件 | 更新日期: 2023-09-27 18:00:47
我试着做一个基本的数字猜谜游戏。然而,我找不到保持随机数不变的方法,以便为用户提供增加或减少猜测的线索。这对你来说很容易,但我需要你有价值和有用的观点。附言:是的,我是个初学者。
while (true)
{
Random rd = new Random();
Console.WriteLine("guess a number 0-10");
guess = int.Parse(Console.ReadLine());
comp = rd.Next(10);
if (guess == comp)
{
Console.WriteLine("congratz!! u won!!");
Console.WriteLine("press any key to exit");
break;
}
// Those are the conditions for directing user.I couldn't implement it.
//else if (guess > comp)
// Console.WriteLine("your guess is greater than my number, yow!");
//else if (guess < comp)
// Console.WriteLine("your guess is lower than my number, yow!");
Console.WriteLine("loser!!" + "My number was:" + comp);
}
Console.ReadLine();
你可能想这样做:
Random rd = new Random();
int comp = rd.Next(10); // Store you random number before the loop
int guess = 0;
int trylimit = 3;
int round = 0;
while (round < trylimit)
{
Console.WriteLine("guess a number 0-10");
guess = int.Parse(Console.ReadLine());
if (guess == comp)
{
Console.WriteLine("congratz!! u won!!");
Console.WriteLine("press any key to exit");
break;
}
else if (guess > comp)
Console.WriteLine("your guess is greater than my number, yow!");
else if (guess < comp)
Console.WriteLine("your guess is lower than my number, yow!");
round++;
}
Console.WriteLine("loser!!" + "My number was:" + comp + "'n");
Console.ReadLine();
正如Tim Schmelter在评论中所说,您需要在while loop
之前声明并初始化random
,否则每次进入循环时都可能发生更改。
正如我在评论中所说,在循环之前声明并初始化Random
实例,否则如果循环执行得很快,您将始终生成相同的数字。这在这里可能不是问题,因为Console.ReadLine()
停止执行,但无论如何都要知道这一点。
如果您想要一个介于0和10之间的数字,则必须使用rd.Next(11)
,因为最大值是独占的。如果用户输入了无效整数,请使用int.TryParse
来防止出现异常。
然后,您可以使用for-loop
只询问用户n次并提供线索。
int numTries = 3;
Random rd = new Random();
while (true)
{
Console.WriteLine("guess a number 0-10");
int number = rd.Next(11); // 0-10
for (int currentTry = 1; currentTry <= numTries; currentTry++)
{
int guess;
if (!int.TryParse(Console.ReadLine().Trim(), out guess))
{
Console.WriteLine("Please enter a valid integer between 0 and 10!");
continue; // next try
}
if (guess == number)
{
Console.WriteLine("congratz!! u won!!");
break; // breaks for-loop
}
else if(currentTry == numTries) // last try
Console.WriteLine("loser!!" + "My number was: " + number);
else if (guess < number)
Console.WriteLine("your guess is lower than my number, yow!");
else if (guess > number)
Console.WriteLine("your guess is greater than my number, yow!");
}
}
在宣布用户为失败者之前,您可以询问用户n次。当然,我没有检查正确的用户输入;小心非数字用户输入!
Random rnd = new Random();
int comp = rnd.Next(10);
int guess;
int numberofGuesses = 3;
int counter = 0;
Console.WriteLine("guess a number 0-10");
do
{
guess = int.Parse(Console.ReadLine());
if (guess == comp)
{
Console.WriteLine("congratz!! u won!!");
Console.WriteLine("press any key to exit");
Console.ReadKey();
return;
}
else
{
if (guess > comp)
{
Console.WriteLine("your guess is greater than my number, yow!");
}
else
{
Console.WriteLine("your guess is lower than my number, yow!");
}
if(counter!=numberofGuesses - 1)
Console.WriteLine("Guess again!");
counter++;
}
} while (counter < numberofGuesses);
Console.WriteLine("loser!!" + "My number was:" + comp);
Console.ReadKey();
return;
}