当某些情况发生时,如何在 while 循环中保持变量常量的随机值?

How can I keep a randomized value of a variable constant in a while loop when certain conditions occur?

我尝试制作一个基本的猜数游戏。但是我找不到保持随机数不变的方法,以便为用户提供增加或减少的线索 his/her guess.This 对您来说很容易,但我需要您的宝贵和有用的观点。 PS: 是的,我是初学者。

 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;
    }