Im not even sure what to call what I want to do... Basically I have a selection screen and use a switch statement instead of an if/else, in the default portion of the switch statement I want to ask a confirm to quit... Basically
switch(input)
{
default:
Console.WriteLine("Are you sure?")
var confirm = Console.ReadLine();
switch (confirm)
{
case "y":
//quit
case "n":
break;
default:
// GO BACK TO Console.WriteLine();
break;
}
break;
}
Basically I need to go back to the Console.WriteLine(); part, but Im not sure what the most efficient method is to do that?
The first switch statement is totally useless as there is only a default.
I do not know what means // GO BACK TO Console.WriteLine();
(WHERE???!) but I guess you want to do something like this:
boolean toExit = false; while (!toExit) { // And for what is the input?? string input = Console.ReadLine(); Console.WriteLine("Are you sure?") var confirm = Console.ReadLine(); switch (confirm) { case "y": toExit = true; break; case "n": break; default: // GO BACK TO Console.WriteLine(); // SO, WHERE? break; }
}
If I want some interactivity in console application I usually create a bunch of Control classes (similar to WinForms, with a bunch of various feature like color/align/etc). To prompt console dialog for simple answer Yes/No, you could use just simple method:
public static bool PromptYesNoDialog(string question)
{
Console.WriteLine(question);
while(true)
{
var answer = Console.ReadLine().Trim().ToLower();
if(answer.Equals("y") || answer.Equals("yes"))
return true;
if(answer.Equals("n") || answer.Equals("no"))
return false;
Console.WriteLine("Answer should be 'y' or 'n'.");
}
}
Then use it:
//doing work, and encountered problem which only user can solve
if(PromptYesNoDialog("Do you wanna play?"))
{
//play
}
else
{
//not play
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.