1

I have try to get input from user for two time for each case like R,S,T. Now I am able to get input in single time only. How can I get input for two times in each case.

When I press S or T or R form should be close automatically.

public class Program
{
    public static void Main()
    {
        string mystring = null; 
        Console.WriteLine("Enter your input : "); 
        mystring = Console.ReadLine();
        switch (mystring)
        {
            case "R": 
                Console.WriteLine("R");
                break; 

            case "S": 
                Console.WriteLine("S");
                break;

            case "T": 
                Console.WriteLine("T");
                break; 
        }        
    }      
}

OLD Output:

Enter your input : 
R
R

Required OUTPUT:

Enter your input : 
R
R
Enter your input : 
S
S
Enter your input : 
T
T

I would except continuously get user input in two or three times.

Note : When I press R,S or T key word form should be closed automatically.

1 Answer 1

1

You should use a loop to get that work, it will prompt you to enter your input until you enter something other than R S T:

bool loopCondition = true;
while (loopCondition)
{
    string mystring = null; Console.WriteLine("Enter your input : ");
    mystring = Console.ReadLine();
    switch (mystring)
    {
        case "R":
        case "S":
        case "T":
            Console.WriteLine(mystring);
            break;
        default:
            Console.WriteLine("Invalid Entry.. Exiting");
            loopCondition = false;
            break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The name of your variable exitCondition is little bit confused when assigned to true at the beginning
Yep ! Agree for loopCondition :)

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.