简体   繁体   中英

C# - Use ReadKey for loop

I have been searching the web for about an hour and I just can't find the answer to my question. I'm very new to programming and I hope I'm not wasting your time. I want my program to loop if I would click "Y", exit if I click "N" and do nothing if I click any other button. Cheers!

Console.Write("Do you wan't to search again? (Y/N)?");
if (Console.ReadKey() = "y")
{
    Console.Clear();
}
else if (Console.ReadKey() = "n")
{
    break;
} 

You have an example here of Console.ReadKey method :

http://msdn.microsoft.com/en-us/library/471w8d85.aspx

//Get the key
var cki = Console.ReadKey();

if(cki.Key.ToString() == "y"){
    //do Something
}else{
    //do something
}

You are missing keystrokes this way. Store the return of Readkey so you can split it out.
Also, comparison in C# is done with == and char constants use single quotes ( ' ).

ConsoleKeyInfo keyInfo = Console.ReadKey();
char key = keyInfo.KeyChar;

if (key == 'y')
{
    Console.Clear();
}
else if (key == 'n')
{
   break;
}

you can use the keychar to check that character is pressed Use can understand that by following example

Console.WriteLine("... Press escape, a, then control X");
// Call ReadKey method and store result in local variable.
// ... Then test the result for escape.
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.Escape)
{
    Console.WriteLine("You pressed escape!");
}
// Call ReadKey again and test for the letter a.
info = Console.ReadKey();
if (info.KeyChar == 'a')
{
    Console.WriteLine("You pressed a");
}
// Call ReadKey again and test for control-X.
// ... This implements a shortcut sequence.
info = Console.ReadKey();
if (info.Key == ConsoleKey.X &&
    info.Modifiers == ConsoleModifiers.Control)
{
    Console.WriteLine("You pressed control X");
}

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 (aka 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM