Methods in C#
/*Please help me to ask the user to input only "yes or no" If they fail to do so, it will ask them again. The condition where i want to loop is the the *default* part of the switch case. meaning if they don't input "yes or no" it will output: Choose either 'yes' or 'no' :-( // here they will input again if false Choose either 'yes' or 'no' :-( // and so on .................... if true then cases will be executed. */ string choice, retry; Console.WriteLine("**THIS MIGHT DAMAGE YOUR PC**\nWould you like continue:\"yes or no\""); choice = Console.ReadLine(); switch (choice) { case "yes": // this is below line is a method. form(); break; case "no": Console.WriteLine("\nThank You :-)"); break; default: Console.WriteLine("\nChoose either 'yes' or 'no' :-("); retry = Console.ReadLine(); break; }
8/26/2019 3:31:51 PM
Adarsh Mamgain
7 Answers
New AnswerHere is the example. You need to use a loop. It will not work on sololearn because of the input issue. Try in an IDE. The formating is bad. I may fix the format or I may not. https://code.sololearn.com/cwlcOmAmlWAG/?ref=app
Thank You so much https://www.sololearn.com/Profile/3685063/ @Manual It really worked :-] and thank you everyone for replying.
HNNX 🐿 That would work. There are options switch conditions and if / else if / else is fine as well.
I would avoid switch statement altogether here and use a while loop to repeat the question if the answer was false
you can also use a simple while loop: while (string !=“yes” && string !=“no”){ console writeline (“only yes/no”); string =console.readline();