C# Guess random number

2019-03-04 11:10发布

问题:

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?

The objective:

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

回答1:

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



回答2:

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.



标签: c# random