+ 2

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

26th Aug 2019, 3:31 PM
Adarsh Mamgain
Adarsh Mamgain - avatar
7 Respostas
+ 2
Here 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
26th Aug 2019, 4:23 PM
Manual
Manual - avatar
+ 3
Thank You so much https://www.sololearn.com/Profile/3685063/ @Manual It really worked :-] and thank you everyone for replying.
26th Aug 2019, 5:36 PM
Adarsh Mamgain
Adarsh Mamgain - avatar
+ 2
HNNX 🐿 That would work. There are options switch conditions and if / else if / else is fine as well.
26th Aug 2019, 5:16 PM
Manual
Manual - avatar
+ 1
Paste the code in the playground, so users do not need do it to help you.
26th Aug 2019, 4:16 PM
Manual
Manual - avatar
+ 1
I would avoid switch statement altogether here and use a while loop to repeat the question if the answer was false
26th Aug 2019, 4:51 PM
HNNX 🐿
HNNX 🐿 - avatar
+ 1
Adarsh Mamgain You are welcome. 😄
26th Aug 2019, 5:42 PM
Manual
Manual - avatar
+ 1
you can also use a simple while loop: while (string !=“yes” && string !=“no”){ console writeline (“only yes/no”); string =console.readline();
20th Oct 2019, 1:00 PM
Sharon Keren
Sharon Keren - avatar