Methods in C# | SoloLearn: Learn to code for FREE!

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

8/26/2019 3:31:51 PM

Adarsh Mamgain

7 Answers

New Answer

+1

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

+3

Thank You so much https://www.sololearn.com/Profile/3685063/ @Manual It really worked :-] and thank you everyone for replying.

+2

HNNX 🐿 That would work. There are options switch conditions and if / else if / else is fine as well.

+1

Paste the code in the playground, so users do not need do it to help you.

+1

I would avoid switch statement altogether here and use a while loop to repeat the question if the answer was false

+1

Adarsh Mamgain You are welcome. 😄

+1

you can also use a simple while loop: while (string !=“yes” && string !=“no”){ console writeline (“only yes/no”); string =console.readline();