Home > Enterprise >  How do I stop the Console.ReadLine() command triggering multiple times in my loop?
How do I stop the Console.ReadLine() command triggering multiple times in my loop?

Time:10-17

I'm a complete newbie when it comes to C#, and I'm sure this has a very simple answer:

I've written a super simple piece of code to enable a user to pop in a number and receive the square of that number back out. I've also enabled the user to decide whether they want to play or not, with Yes, No, or 'else' triggering different outcomes.

The 'Yes' input works just fine, with the program working as expected. However, the 'No' or 'else' response seems to require the user to enter a response a second time before triggering the outcome properly.

I can't see why it would be looking for a response twice in a row, so if you can offer any help I'd really appreciate it.

Here's the code I'm using:

namespace Playground { internal class Program { static void Main(string[] args) { Console.WriteLine("If you enter a number, I'll report back the number squared. Would you like to play? Enter Yes or No");

        int x = 100;
        while (x == 100)
        {
            
            if (Console.ReadLine() == "Yes")
            {
                Console.WriteLine("Great! Enter a number");
                int initial = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(initial * initial);
                Console.WriteLine("Would you like to try again?");
                
            }

            else if(Console.ReadLine() == "No")
            {
                Console.WriteLine("Ok, goodbye! Press any key to Exit");
                Console.ReadLine();
                Environment.Exit(0);
            }

            else
            {
                Console.WriteLine("Sorry, I didn't understand that. Please enter Yes or No");
            }
        }
        
    }
}

}

CodePudding user response:

When running into the first if the condition run the function Console.ReadLine() so you have to write the answer. This is going to happen also in the else if (Console.ReadLine() == "No").

What you have to do is save the user input only once and decide what to do from that answer.

The code is:

    int x = 100;
    while (x == 100)
    {
        string answer = Console.ReadLine();
        if (answer == "Yes")
        {
            Console.WriteLine("Great! Enter a number");
            int initial = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(initial * initial);
            Console.WriteLine("Would you like to try again?");
            
        }

        else if(answer == "No")
        {
            Console.WriteLine("Ok, goodbye! Press any key to Exit");
            Console.ReadLine();
            Environment.Exit(0);
        }

        else
        {
            Console.WriteLine("Sorry, I didn't understand that. Please enter Yes or No");
        }
    }

You can also use the switch statement:

int x = 100;
while (x == 100){
    string answer = Console.ReadLine();
    switch (answer){
        case "Yes":
            Console.WriteLine("Great! Enter a number");
            int initial = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(initial * initial);
            Console.WriteLine("Would you like to try again?");
            break;
        case "No":
            Console.WriteLine("Ok, goodbye! Press any key to Exit");
            Console.ReadLine();
            Environment.Exit(0);
            break;
        default:
            Console.WriteLine("Sorry, I didn't understand that. Please enter Yes or No");
            break;
    }
}
  •  Tags:  
  • c#
  • Related