Karthick
Karthick

Reputation: 33

get input from keyboard using C#?

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.

Upvotes: 1

Views: 11630

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29006

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

Upvotes: 1

Related Questions