c# - Console.ReadLine() 替代方案

标签 c# console key

我有以下控制台应用程序:

private static bool run = false;

static void Main(string[] args)
{
    int choice = 0;  

    while (!run) 
    {
        Console.WriteLine("\n\t\t Press '1', '2' or '3' to continue");

        choice = int.Parse(Console.ReadLine());

        switch (choice)
        {
            case 1:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 1");
                }
            case 2:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 2");
                }
            case 3:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t you pressed 3");
                }
            default:
                {
                    Console.Clear();
                    Console.WriteLine("\n\t\t Invalid key");
                    break;
                }
        }
    }

    Console.ReadLine();
}

但是,使用这段代码:

choice = int.Parse(Console.ReadLine()); 

我希望这样当用户按下 1 或 2 或 3 键时,立即执行相应的代码,而不是在按下所需的键后必须按下回车键。有哪些替代方案?

非常感谢任何帮助/指导/提示, 谢谢

最佳答案

ReadKey怎么样?那么呢?

关于c# - Console.ReadLine() 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9106027/

相关文章:

c# - 正则表达式中的双重否定回顾

c# - 处理服务结构中的异常

batch-file - Windows : script/program for USB key that opens a console, 并设置路径和工作目录

c++ - 如何查看到控制台的长 C++ 输出(仅显示结束部分)

python - 他们如何在 Django 项目中的 python 控制台中运行这些命令?

mysql - SQL 查找下一个可用的字符串键

javascript - 为什么这个返回哈希给我带来问题?

c++ - std::map 比较指针

从通用抽象类派生的类的 C# 方法重载问题

c# - 为什么 NuGet 会下载看似不必要的依赖项?