C#猜测随机数 [英] C# Guess random number

查看:145
本文介绍了C#猜测随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将标识符更改为英语,以便可以在Code Review上轻松理解它.我使用了重构,因此它在任何地方都可以立即更改,但是那时候它出错了.我的代码不再起作用.代码未完成.我仍然需要找到一种重新玩的方法.我在这里做错什么了?

I changed the identifiers to English so it can be easily understood on Code Review. I used refactor so it changes everywhere instantly but that's when it went wrong. My code does not work anymore. The code is not completed though. I still need to find a way to play again. What am i doing wrong here?

目标:

创建一个控制台应用程序,您需要在其中猜测1到100之间的随机生成的数字.当您的猜测低于生成的数字时,它将显示更高".如果它高于生成的数字,它将说更低.检查代码中的注释.

Create a console application where you'll need to guess the random generated number that is between 1 and 100. When your guess is below the generated number, it will say Higher. If it's higher than the generated number, it will say lower. Check the comments in the code.

 public static void Main(string[] args)
 {
     const int STOPVALUE = 0;
     int inputNumber, count = 0;

     Random generator = new Random();

     int gameNumber = generator.Next(1, 100);

     //I output the game number to see beforehand want the winning number is.
     //The generator is doing it's job right.
     Console.WriteLine(gameNumber);

     //Here is where it goes wrong and i can't seem to understand why.
     //It keeps saying go higher even when the input number is 100.
     do
     {
         Console.Write("(Stop Value: 0) Insert a number: ");
         inputNumber = int.Parse(Console.ReadLine());

         count++;

         if (gameNumber == inputNumber)
         {
             Console.WriteLine("Your guess is correct.");
             Console.WriteLine("Number of turns: " + count);

             count = 0;

             Console.WriteLine("Play again? (Y/N)");
             char startOver = char.Parse(Console.ReadLine());

             if (startOver == 'Y')
             {
                 return;
             }

             if (startOver == 'N')
             {
                 Environment.Exit(0);
             }
         }

         if (inputNumber > gameNumber)
         {
             Console.WriteLine("Higher");
         }

         if (inputNumber < gameNumber)
         {
             Console.WriteLine("Lower");
         }
     } 
     while (inputNumber != STOPVALUE);
 }

推荐答案

正如皮科(Pikoh)所说,只需反转条件即可.尝试分解代码并亲自查看:

As Pikoh said, just invert the conditions. Try breaking down the code and see it for yourself:

比方说,游戏生成了一个秘密数字42. 假设您选择了数字50.

Let's say the game generated a secret number 42. Let's say you chose the number 50.

然后您会看到这种情况:

Then you'll see this condition:

if (inputNumber > gameNumber)
{
    Console.WriteLine("Higher");
}

if (inputNumber < gameNumber)
{
    Console.WriteLine("Lower");
}

因此它将被解释"为:

if (50 > 42)
{
    Console.WriteLine("Higher");
}

if (50 < 42)
{
    Console.WriteLine("Lower");
}

如您所见,您的逻辑不正确.当数字低时,它告诉您降低数字;当数字低时,它指示您降低数字.而当数字高时,它会告诉您走得更高.

As you can see your logic is not correct. When the number is low it tells you to go lower; and when the number is high it tells you to go higher.

您也可以参考以下答案: https://stackoverflow.com/a/46665389/4352946

You may also reference to this answer: https://stackoverflow.com/a/46665389/4352946

这篇关于C#猜测随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆