【问题标题】:How can I keep a randomized value of a variable constant in a while loop when certain conditions occur?当某些情况发生时,如何在 while 循环中保持变量常量的随机值?
【发布时间】:2015-09-18 07:50:55
【问题描述】:

我尝试制作一个基本的猜数字游戏。但是我找不到保持随机数不变的方法,以便为用户增加或减少他/她的猜测提供线索。这对你来说很容易,但我需要你有价值和有用的观点。 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实例,否则如果循环执行非常快,你总是会生成相同的数字。
  • 从循环前设置comp值开始。
  • 干得好。感谢您的 cmets :)
  • 作为初学者,我建议您查看变量范围。这将帮助您自己回答此类问题。

标签: c# random while-loop


【解决方案1】:

你可能想要这样:

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,否则它可能会在您每次进入循环时发生变化。

【讨论】:

  • 感谢您的帮助@Bladepianist
  • 这并没有考虑到OP想要给用户提供线索。 “给用户增加或减少他/她的猜测的线索”它总是在第一次尝试后停止并告诉使下一次猜测毫无意义的数字。
  • 这就是我编辑它的原因,尽管它现在看起来有点像其他人发布的。
【解决方案2】:

正如我在评论中所说,在循环之前声明并初始化 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!");
    }
}

【讨论】:

    【解决方案3】:

    在宣布他失败之前,您可以询问用户 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2014-06-04
      • 2021-10-19
      • 2013-10-29
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多