Skip to content

Instantly share code, notes, and snippets.

@ebenito
Created March 22, 2019 08:22
Show Gist options
  • Save ebenito/3bc8883642bd23a2f7c78f09fdc28e41 to your computer and use it in GitHub Desktop.
Save ebenito/3bc8883642bd23a2f7c78f09fdc28e41 to your computer and use it in GitHub Desktop.
Forzar una respuesta correcta por consola
//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();
@ebenito
Copy link
Author

ebenito commented Mar 22, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment