简体   繁体   English

C# - 使用 ReadKey 进行循环

[英]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.我一直在搜索 web 大约一个小时,但我找不到我的问题的答案。 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.如果单击“Y”,我希望我的程序循环,如果单击“N”则退出,如果单击任何其他按钮,则不执行任何操作。 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 : 您在此处有Console.ReadKey方法的示例:

http://msdn.microsoft.com/en-us/library/471w8d85.aspx 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. 存储Readkey的返回值,以便将其拆分。
Also, comparison in C# is done with == and char constants use single quotes ( ' ). 另外,C#中的比较是通过==完成的,而char常量使用单引号( ' )。

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 您可以使用keychar来检查是否按下了字符使用以下示例可以理解

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:您可以使用do 语句(又名do loop )在某些条件为真时重复某些代码,例如:

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).我在!validKey中使用了“not”,因为这样读起来更好:do {this code} while(用户没有按下有效键)。 You might prefer to use a while loop if you think the code reads better with that construction.如果您认为使用该结构的代码可读性更好,您可能更喜欢使用while循环。

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; .ToLower(System.Globalization.CultureInfo.InvariantCulture)位使得按下“y”或“Y”无关紧要,它很有可能处理任何字母,即使是那些有意外的大写/小写变化; see Internationalization for Turkish: Dotted and Dotless Letter "I" and What's Wrong With Turkey?请参阅土耳其语的国际化:带点和不带点的字母“I”和土耳其有什么问题? for explanation of why it's a good idea to be careful about that kind of thing.解释为什么小心这种事情是个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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