Home > database >  How do I repeat a question in a switch case
How do I repeat a question in a switch case

Time:05-02

I'm trying to make a question repeat if the input isn't right. Here's the code:

Console.WriteLine("choose a name");
string userInput = Console.ReadLine();
Boolean input = true;


switch (userInput)
{
    case "joe":
        Console.WriteLine("you chose a joe");
        break;

    case "bob":
        Console.WriteLine("you chose a bob");
        break;

}

How do I make it if it isn't one of the two answers it reasks the question?

CodePudding user response:

Use the default case.

If none of the cases in a switch statement match, the default case runs. Here's an example.

Console.WriteLine("choose a name");
string userInput = Console.ReadLine();

switch (userInput)
{
    case "joe":
        // ...
        break;

    case "bob":
        // ...
        break;

    default:
        // This runs if userInput is neither "joe" nor "bob"
}

Then you can make a method that writes choose a name to the console, takes the user's input, and runs the switch statement - and the default case would call the same method.

void GetName()
{
    // Write "choose a name" to the console
    // Take the user's input
    
    switch (userInput)
    {
        case "joe":
            // ...
            break;

        case "bob":
            // ...
            break;

        default:
            GetName();
            return;
    }
}

CodePudding user response:

You need a loop (either while or do-while) to repeat the iteration but not a switch-case.

While switch-case is used to control the flag (isCorrectInput) that stops the loop.

bool isCorrectInput = false;

do
{
    Console.WriteLine("choose a name");
    string userInput = Console.ReadLine();


    switch (userInput)
    {
        case "joe":
            Console.WriteLine("you chose a joe");
            isCorrectInput = true;
            break;

        case "bob":
            Console.WriteLine("you chose a bob");
            isCorrectInput = true;
            break;
    }
} while (!isCorrectInput);

Reference

Iteration statement

CodePudding user response:

You should use a bool variable, that will determine whether you need to repeat the logic or not. Combine it with a loop and you get this:

        bool keepAsking = true;
        while (keepAsking)
        {
            Console.WriteLine("choose a name");
            string userInput = Console.ReadLine();
            Boolean input = true;


            switch (userInput)
            {
                case "joe":
                    Console.WriteLine("you chose a joe");
                    keepAsking = false;
                    break;

                case "bob":
                    Console.WriteLine("you chose a bob");
                    keepAsking= false;
                    break;
            }
        }

CodePudding user response:

You can try something like this. BUt you need to slightly change your code.

class XYZ
    {
        static void Main(string[] args)
        {
            GetData()
        }
    }


static void GetData()
{
  string userInput = Console.ReadLine();
  checkcondition(userInput);
}

static void checkcondition(userInput)
{
switch (userInput)
{
    case "joe":
        Console.WriteLine("you chose a joe");
        break;

    case "bob":
        Console.WriteLine("you chose a bob");
        break;
    default:
        GetData();
        break;
}
}

CodePudding user response:

You can also achieve the same results using a smaller code but with a while loop instead of switch statement

while(true){
    Console.Write("Choose a Name: ");
    var name = Console.ReadLine().ToLower();
    if(name == "joe" || name == "bob"){
        Console.WriteLine("You chose a "   name   "!");
        break;
    }else{
        Console.WriteLine("Invalid Selection, Try Again");
    }
}
  • Related