首页 > 解决方案 > 在 1-10 之间选择一个数字 游戏每轮后不能产生不同/新的数字

问题描述

我正在尝试开发一款应用程序/小游戏,该游戏旨在尝试猜测与程序相同的 1-10 之间的相同数字并获胜,但在那轮之后你应该能够再次玩!我的问题是程序每次都会生成相同的数字。我已经浏览了这里的其他线程,但它们似乎都没有帮助,或者至少我无法理解它们。我怎样才能解决这个问题?

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

标签: c#randomrandom-seed

解决方案


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

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

推荐阅读