选择 1-10 之间的数字游戏不能在每轮后生成不同/新数字

Choose a number between 1-10 game can't generate different / new number after each round

我正在尝试开发一个 app/small 游戏,该游戏是关于尝试猜测与程序相同的 1-10 之间的相同数字并获胜,但在那轮之后你应该能够再玩一次!我的问题是程序每次都生成相同的数字。我浏览了这里的其他主题,其中 none 似乎有帮助,或者至少我无法理解它们。我该如何解决这个问题?

static void Main(string[] args)
{
   Random random = new Random();
   bool play = true;

   int playnum = random.Next(1, 11);

   while (play)
   {
      Console.Write("\n\tGuess number between 1-10 ");

      int numb;
      bool success = Int32.TryParse(Console.ReadLine(), out numb);

      if (success)
      {
         if (numb < playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tinput number " + numb + " too small, try again.");
         }

         if (numb > playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tinput number " + numb + " too big, try again.");
         }

         if (numb == playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tcongrats!");

            bool LoopBreaker = true;
            do
            {
               Console.WriteLine("continue or finish?");
               string input = Console.ReadLine();
               char CharFromTryPass = ' ';
               bool TryparsResult = char.TryParse(input, out CharFromTryPass);

               bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
               if (IsValidChar)
               {
                  Console.WriteLine(" please write A or F!");
               }

               else if (TryparsResult)
               {

                  if (CharFromTryPass == 'A')
                  {
                     Console.WriteLine("see you!");
                     play = false;
                     LoopBreaker = false;
                  }

                  if (CharFromTryPass == 'F')
                  {
                     play = true;
                     LoopBreaker = false;
                  }
               }

            } while (LoopBreaker);

你的代码有点乱,你应该把它分解成更小的方法。但基本上你只需要在这个人猜对之后生成一个新数字。所以在“恭喜!”之后加上这个线.

playnum = random.Next(1, 11);

.

static void Main(string[] args)
        {
            Random random = new Random();
            bool play = true;

            int playnum = random.Next(1, 11);

            while (play)
            {
                Console.Write("\n\tGuess number between 1-10 ");

                int numb;
                bool success = Int32.TryParse(Console.ReadLine(), out numb);

                if (success)
                {
                    if (numb < playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tinput number " + numb + " too small, try again.");
                    }

                    if (numb > playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tinput number " + numb + " too big, try again.");
                    }

                    if (numb == playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tcongrats!");
                        playnum = random.Next(1, 11);

                        bool LoopBreaker = true;
                        do
                        {
                            Console.WriteLine("continue or finish?");
                            string input = Console.ReadLine();
                            char CharFromTryPass = ' ';
                            bool TryparsResult = char.TryParse(input, out CharFromTryPass);

                            bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
                            if (IsValidChar)
                            {
                                Console.WriteLine(" please write A or F!");
                            }

                            else if (TryparsResult)
                            {

                                if (CharFromTryPass == 'A')
                                {
                                    Console.WriteLine("see you!");
                                    play = false;
                                    LoopBreaker = false;
                                }

                                if (CharFromTryPass == 'F')
                                {
                                    play = true;
                                    LoopBreaker = false;
                                }
                            }

                        } while (LoopBreaker);
                    }
                }
            }
        }

您无法生成新号码,因为您在代码中只调用了一次随机数。 将 int playnum = random.Next(1, 11); 放在循环中,应该没问题。正如 Nyssa 已经说过的,您可能应该使用调试器来了解您的代码做错了什么。