我不明白我在这里缺少什么,但它似乎Console.ReadKey()
仍然处于活动状态并且导致控制台让我Console.ReadLine()
在调用后使用时输入两次Console.ReadKey()
。
一旦做出选择,我已经上下搜索了如何逃脱ReadKey()
,但无济于事。
为了澄清,这是出乎意料的行为:当控制台弹出时,作为示例向用户呈现这三个选项。当用户随后键入“u”或“h”时,控制台不会等待;它会立即执行操作,而无需用户按下Enter。
我做错了什么吗?
static void Main(string[] args)
{
Console.WriteLine("u up");
Console.WriteLine("h home");
Console.WriteLine("x exit");
Console.WriteLine("---------------------------------");
Console.WriteLine(" [Enter Your Selection]");
Console.WriteLine("---------------------------------");
Console.Write("Enter Selection: ");
ConsoleKeyInfo selection;
Console.TreatControlCAsInput = true;
int value;
selection = Console.ReadKey();
if (char.IsDigit(selection.KeyChar))
{
value = int.Parse(selection.KeyChar.ToString());
value -= 1;
Console.WriteLine("You've entered {0}", value);
}
else
{
switch (selection.Key)
{
case ConsoleKey.U:
blurp();
break;
case ConsoleKey.H:
blurp();
break;
case ConsoleKey.X:
System.Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid Input...");
break;
}
}
}
public static void blurp()
{
Console.WriteLine("");
Console.Write("Enter Another Value: ");
string value = Console.ReadLine();
Console.WriteLine("You've entered {0}", value);
}