C# Guess random number

后端 未结 2 1547
不知归路
不知归路 2021-01-29 05:27

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 do

相关标签:
2条回答
  • 2021-01-29 05:50

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

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

    So it would be "interpreted" as:

    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.


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

    0 讨论(0)
  • 2021-01-29 06:08

    To play the game again: When the user press Y, you are calling return, which exits the function. You could just put an infinite loop (like while(true)) wrapping the code from int gameNumber = generator.Next(1, 100); to the end, then change that return to a break. That will exit the inner loop and go back to the new one, thus generating a new number and starting all over again.

    0 讨论(0)
提交回复
热议问题