-1

给出了以下场景:

出现欢迎屏幕。如果用户已阅读欢迎文本,他有 2 个选择:

a) 按 ENTER 继续获取下一个文本

b) 按 E-Key 退出程序

所以我的问题是:

如何检查用户是否按下了 ENTER 键?

到目前为止我尝试过的 - 就像非常原始的原型一样

...var userInput= Console.ReadLine();

    if (userInput == "\r")
    {
        Console.WriteLine("correct");
    }
    else
    {
        Console.WriteLine("wrong");
    }....

我也通过正则表达式尝试了它,但我没有让它运行。感谢您的帮助...

4

1 回答 1

0

就像这样(带Console.ReadKey):

static void Main(string[] args)
{
    Console.WriteLine("Hello Mr.Sun! Try press enter now:");
    var userInput = Console.ReadKey();
    if(userInput.Key == ConsoleKey.Enter)
    {
        Console.WriteLine("You pressed enter!");
    } else
    {
        Console.WriteLine("You pressed something else");
    }
}
于 2021-01-23T20:52:55.700 回答