C#-使用ReadKey进行循环

C#-使用ReadKey进行循环,c#,loops,while-loop,readkey,C#,Loops,While Loop,Readkey,我在网上搜索了大约一个小时,但我就是找不到我问题的答案。我对编程非常陌生,希望我没有浪费你的时间。如果单击“Y”,我希望程序循环;如果单击“N”,则退出;如果单击任何其他按钮,则不执行任何操作。干杯 Console.Write("Do you wan't to search again? (Y/N)?"); if (Console.ReadKey() = "y") { Console.Clear(); } else if (Console.ReadKey() = "n") { b

我在网上搜索了大约一个小时,但我就是找不到我问题的答案。我对编程非常陌生,希望我没有浪费你的时间。如果单击“Y”,我希望程序循环;如果单击“N”,则退出;如果单击任何其他按钮,则不执行任何操作。干杯

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

这里有一个Console.ReadKey方法的示例:


这里有一个Console.ReadKey方法的示例:


这样您就错过了击键。存储Readkey的返回值,以便将其拆分。
此外,C#中的比较是通过
=
完成的,char常量使用单引号(


这样您就错过了击键。存储Readkey的返回值,以便将其拆分。
此外,C#中的比较是通过
=
完成的,char常量使用单引号(


您可以使用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");
}

您可以使用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");
}

那么它现在做什么呢?它做什么了?现在它做什么了?它有什么作用?
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");
}