【问题标题】:How to make the console accept input from the Enter key only?如何使控制台仅接受来自 Enter 键的输入?
【发布时间】:2021-07-15 03:22:53
【问题描述】:

我想让 C# 控制台只接受来自启动屏幕上 Enter 键的输入。

我已经做到了,当按下 Enter 键以外的任何东西时,它都会关闭控制台。

我怎样才能使控制台只接受来自 Enter 键的输入,这样当我按下其他任何东西时应用程序就不会关闭,然后再接收正常输入?

class Program
{
    public static void ClearKeyBuffer()
    {
        while (Console.KeyAvailable)
            Console.ReadKey(true);
    }
    public static void Main (string[] args)
    {
        int attempts = 0;
        int displayattempts = 5;
        bool validentry;
        Console.WriteLine("Please press enter to begin");
        var key = System.Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
        {
            while (attempts < 5)
            {
                string input;
                attempts = (attempts + 1);
                Console.Clear();
                Console.WriteLine("Please wait...");
                Thread.Sleep(5000);
                Console.Clear();
                Console.WriteLine("Please enter your user number.");
                Console.WriteLine("Attempts Remaining:" + displayattempts);
                ClearKeyBuffer();
                Console.WriteLine(" ");
                input = Console.ReadLine();
                {
                    if (input == "5573")
                    {
                        validentry = true;
                    }

                    else
                    {
                        validentry = false;
                    }

                    if (validentry == false)
                    {
                        displayattempts = (displayattempts - 1);
                        Console.Clear();
                        Console.WriteLine("Error: Invalid number ID entered. Please wait 5 
                        seconds, and try again.");
                        Thread.Sleep(5000);
                    }

                    else if (validentry == true)
                    {
                        Console.Clear();
                        Console.WriteLine("Welcome Samuel");
                        ValidUserEntry();
                    }
                }
            }
        }
        if (displayattempts == 0)
        {
            Console.Clear();
            Console.WriteLine("Error: You have entered the wrong number ID too many times. 
             This system will now close in 5 seconds...");
            Thread.Sleep(5000);
            Environment.Exit(0);
        }
    }

    public static void ValidUserEntry()
    {
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("Please wait...");
        Thread.Sleep(5000);
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("What would you like to do?");
        Console.ReadLine();
    }
}

【问题讨论】:

    标签: c# console console-input


    【解决方案1】:

    在第一个 if 之前添加这一行。然后删除if 语句和var key... 行。

    while (Console.ReadKey(true).Key != ConsoleKey.Enter);
    

    替代的,更详细的版本:

    ConsoleKeyInfo key;
    do
    {
        key = Console.ReadKey(true);
    } while (key.Key != ConsoleKey.Enter);
    

    【讨论】:

    • 我在您的答案中添加了一个更详细的版本,因为您的答案可能会让初学者有些困惑。我希望你不介意:)
    • 没关系。谢谢:)
    • 因此,为了澄清起见,您将按下的键存储在 key 变量中,并根据按下的键是 Enter 键的条件使用它?并使其循环启动屏幕,直到按下 Enter 键。
    • @samuelbachet 这正是第二个代码块中发生的情况。在第一个代码块中也发生了同样的情况,除了 ConsoleKeyInfo 值没有存储在变量中并且它使用 empty while loop 可以写为while (someCondition) { }while (someCondition) ;。这是一个 relevant post 关于空的 while 循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2020-07-23
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多