Presumably by "do nothing" you intend that the user be asked to try pressing a valid key until they do. You can use a do statement (a.k.a. do loop) to repeat some code while some condition is true, for example:
var validKey = false;
var searchAgain = false;
do
{
Console.Write("Do you want to search again? (Y/N): ");
string key = Console.ReadKey().KeyChar.ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine();
if (key == "y")
{
validKey = true;
searchAgain = true;
}
else if (key == "n")
{
validKey = true;
searchAgain = false;
}
else
{
Console.WriteLine("Please press the 'Y' key or the 'N' key.");
}
} while (!validKey);
// Now do something depending on the value of searchAgain.
I used the "not" in !validKey
because it reads better that way: do {this code} while (the user hasn't pressed a valid key). You might prefer to use a while
loop if you think the code reads better with that construction.
The .ToLower(System.Globalization.CultureInfo.InvariantCulture)
bit is so that it doesn't matter if "y" or "Y" is pressed, and it has a very good chance of working with any letter, even the ones that have unexpected upper/lower case variations; see Internationalization for Turkish:
Dotted and Dotless Letter "I" and What's Wrong With Turkey? for explanation of why it's a good idea to be careful about that kind of thing.