2

I'm attempting to get a more immediate response to the start of my program, this is rough and not a finished product by any means.

When I attempt to enter "Y" or "N" as per the prompt, I have to enter it several times for the console to recognize the input and spit out the appropriate response.

I'd prefer if it only took one input entry to output the respective response. As of right now, this is not the case. I have also attached console output to put into perspective. As you can see, the console wants me to input Y twice, rather than once which is what I intend for it to do (same goes for N).

Code:

internal static class Program
{
   public static void Main()
   {
        Console.WriteLine("Hello, Simon!");
        Console.WriteLine("Shall we begin?, Please Enter: Y/N");
    
        Console.ReadLine();
    
        if (Console.ReadLine() == "Y")
        {
            Console.WriteLine("Alright, Here we go!");
        }
        else if (Console.ReadLine() == "N")
        {
            Console.WriteLine("Adios Simon!");
        }
    }
}

Console:

Hello, Simon! Shall we begin?, Please Enter: Y/N

Y

Y

Alright, Here we go!

I've attempted to rectify my issue with the forementioned code with either using line breaks or additional Console.ReadLine(); code, but nothing has made the mentioned issue disappear, as of right now my input still is required more than once to get a output.

For example:

Y

Y

is required for an response in the console, id like to make it look more like this:

Hello, Simon!

Shall we begin?, Please Enter: Y/N

"Y"

Alright, Here we go!

3

2 Answers 2

3

Every time you use Console.ReadLine(), you're requesting to read from the console once.

The solution is to have a single Console.ReadLine() and assign it to a variable:

string input = Console.ReadLine();

if (input == "Y")
{
    //...
}
else if (input == "N")
{
    //...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, my dumbass forgot I could do something like this...been a while since i last coded, started this last night
@SimoneQueen if this solves your question, please mark it as accepted answer
2

well the top answer is pretty good, but to help you a little more, you could use the variable tipe char, which only uses one letter, that would help you better

ex.

 char onlyonecaracter = Console.ReadLine();

if (onlyonecaracter == 'Y') // use single quote for the chart value
{
  //do the thing you want to do 
}
else if (onlyonecaracter == 'N')
{
// do the thing you want to do 
}
else
{
Console.WriteLine("Invalid option");
}

as you can see not it also has an option for a wrong input. The char value is good for you since it is more efficient for this case, hope it helps you brother, bless

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.