【问题标题】:How to count users answers in Console如何在控制台中计算用户答案
【发布时间】:2020-03-13 12:06:04
【问题描述】:

使用 C# 的第一天已经卡住了。我的问题是如何计算用户给出的所有答案以找出随机数是什么?然后用正确的答案把它放在最后一句。这就是“测验”的样子:

public static void Main(string[] args)
    {
        Console.WriteLine("The secret number\n");

        Random randomerare = new Random();
        int slump_tal = randomerare.Next(1, 101);

        Console.WriteLine("Write your number and we'll see where it lands:\n");
            string user_nr = Console.ReadLine();
            int tal = Convert.ToInt32(user_nr);
            Console.WriteLine();

            while (tal > slump_tal)
        {
                Console.WriteLine("Wrong! Your number is too big!\n");
                user_nr = Console.ReadLine();
                tal = Convert.ToInt32(user_nr);
        }
            while (tal < slump_tal)

        {
            Console.WriteLine("Wrong Your number is too low!\n");
                user_nr = Console.ReadLine();
                tal = Convert.ToInt32(user_nr);
        }

            while (tal == slump_tal)
            {
        Console.WriteLine("Bravo! That's the correct number\n");
            break;
            }

        Console.WriteLine("The secret number was: {0}\n\nPush the button to finish", slump_tal);

        Console.ReadKey(true);
    }

【问题讨论】:

    标签: c# counter


    【解决方案1】:

    您的代码中有两个步骤。你把它们弄混了。

    第 1 步:执行 while 循环以不断获取用户的输入。

    第 2 步:在每个循环中,您需要根据数字验证输入。

    它应该是 1 个大的 while() 循环和 3 个 if ()。如果您需要示例代码,请告诉我。

    【讨论】:

      【解决方案2】:

      我会这样做:

       1. Get a number
       2. While the user is not hitting the right number:
            2.1 let the user know if the number was too big or too small
            2.2 ask for another number
            2.3 count number-of-attems + 1
       3. If the user arrived here, it means it has the number right, print number-of-attems
      

      在代码中,这可能如下所示:

          public static void Main(string[] args)
          {
              Console.WriteLine("The secret number\n");
      
              Random randomerare = new Random();
              int slump_tal = randomerare.Next(1, 101);
      
              Console.WriteLine("Write your number and we'll see where it lands:\n");
              Console.WriteLine();
      
      
              int user_nr = Convert.ToInt32(Console.ReadLine());
      
              // Here you can store the attempts
              int attempts = 1;
      
              // While the user provides the wrong answer, we iterate
              while(slump_tal != user_nr)
              {
      
                  // We add 1 to the counter
                  attempts++;
      
                  if (slump_tal > slump_tal)
                  {
                      Console.WriteLine("Wrong Your number is too low!\n");
                  }
                  else 
                  {
                      Console.WriteLine("Wrong! Your number is too big!\n");
                  }
      
                  user_nr = Convert.ToInt32(Console.ReadLine());
              }
      
              Console.WriteLine($"Bravo! That's the correct number. you did it in {attempts} attemps");
              Console.WriteLine("The secret number was: {0}\n\nPush the button to finish", slump_tal);
              Console.ReadKey(true);
          }
      

      【讨论】:

      • 嗨@Lez,如果这有助于您理解如何做,请将其标记为答案,以便其他人可以从中受益......如果这还不够,请告诉我们,以便我们可以为您量身定制更好的答案...欢迎来到 StackOverflow! :)
      • 对于标记为-1的人,请留下评论原因,以便我下次提供更好的答案。谢谢!
      • @CarlosGarcia 不是我的反对票,而是我的猜测:因为您的回答缺乏任何解释。您只需发布一个“固定”版本。上面的亚历克斯回答解释了问题,并让(如前所述:'仍在学习')OP,找出问题所在。不要误会我的意思:感谢您在此板上提供帮助!
      • Tahnks @nilsK,我不在乎投反对票,我的股票已经下跌得够多了:D 我关心的是了解原因。我尝试用 cmets 编写自我解释的代码。但你是对的,一些背景会有所帮助,特别是对初学者。我也赞成以前的答案:)。感谢您的评论!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2015-04-14
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多