Home > Mobile >  is their any way to loop an if statement if it does not meet a specific requirement
is their any way to loop an if statement if it does not meet a specific requirement

Time:10-04

        Console.WriteLine("ALL WORDS YOU TYPE MUST BE LOWERCASE UNLESS STATED OTHERWISE, DO 
        YOU UNDERSTAND? \n \n YES | NO \n");
        string answer = "undefined";

        userInput();
        answer = Console.ReadLine();
        userInputDone();


        if (answer == "yes")
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nGreat :)\n");
            Console.ForegroundColor = ConsoleColor.Gray;
        } 
        else if (answer == "no")
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\nTOO BAD\n");
            Console.ForegroundColor = ConsoleColor.Gray;
        }


        Console.WriteLine("What would you lke to do? \n \n PLAY | INSTRUCTIONS");

I'm working on a text-based game to get used to C#, how would i go about looping the if statement so that if the user does not type in yes or no it writes "invalid input, try again" and restarts from the first "Console.WriteLine()"

CodePudding user response:

You can use do while loops: https://www.tutorialsteacher.com/csharp/csharp-do-while-loop

So you basically can do

do {
   // Ask for your input
} while(answer == "no");

CodePudding user response:

string answer = "undefined";
do
{
  Console.WriteLine("ALL WORDS YOU TYPE MUST BE LOWERCASE UNLESS STATED OTHERWISE, DO YOU UNDERSTAND? \n \n YES | NO \n");
  answer = Console.ReadLine();
} while (answer != 'yes' && answer != 'no');
  • Related