-
-
Save ebenito/3bc8883642bd23a2f7c78f09fdc28e41 to your computer and use it in GitHub Desktop.
Forzar una respuesta correcta por consola
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ver: https://stackoverflow.com/questions/37359161/how-would-i-make-a-yes-no-prompt-in-console-using-c | |
bool confirmed = false; | |
string Key; | |
do { | |
Console.Write("Please enter a login key: "); | |
Key = Console.ReadLine(); | |
Console.WriteLine("You entered, " + Key + " as your login key!"); | |
ConsoleKey response; | |
do | |
{ | |
Console.Write("Are you sure you want to choose this as your login key? [y/n] "); | |
response = Console.ReadKey(false).Key; // true is intercept key (dont show), false is show | |
if (response != ConsoleKey.Enter) | |
Console.WriteLine(); | |
} while (response != ConsoleKey.Y && response != ConsoleKey.N); | |
confirmed = response == ConsoleKey.Y; | |
} while (!confirmed); | |
Console.WriteLine("You chose {0}!", Key); | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muestra de ejecución:
Please enter a login key: potato
You entered, potato as your login key!
Are you sure you want to choose this as your login key? [y/n] a
Are you sure you want to choose this as your login key? [y/n] s
Are you sure you want to choose this as your login key? [y/n] l
Are you sure you want to choose this as your login key? [y/n] d
Are you sure you want to choose this as your login key? [y/n] k
Are you sure you want to choose this as your login key? [y/n] f
Are you sure you want to choose this as your login key? [y/n] d
Are you sure you want to choose this as your login key? [y/n] n
Please enter a login key: banana
You entered, banana as your login key!
Are you sure you want to choose this as your login key? [y/n] y
You chose banana!