You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#实现键盘方向键选择菜单时按键无响应问题求助

问题排查与修复方案

核心问题

你代码中所有菜单渲染的if分支里都多余添加了Console.ReadLine()语句,该方法会阻塞程序运行,必须等待用户输入回车后才会继续向下执行,因此你下方监听按键的Console.ReadKey()逻辑根本不会被触发,这就是方向键、回车键没有响应的根本原因。

其他次要问题

  • 菜单选项数组menuOptions里的第三个选项拼写错误:Serivce应为Service
  • 菜单渲染逻辑冗余,不需要为每个选中状态单独写分支,可通过循环遍历数组判断选中状态简化代码

修复后的完整代码

using System;

namespace uppdrag_2.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            // 修正选项拼写错误
            string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Service\t", "Reparation", "Garantie" };
            int menuSelect = 0;

            while (true)
            {
                Console.Clear();
                Console.CursorVisible = false;
                Console.WriteLine("Hello and welcome! Please choose type of registration:");

                // 优化菜单渲染逻辑,避免重复代码
                for (int i = 0; i < menuOptions.Length; i++)
                {
                    if (i == menuSelect)
                    {
                        Console.WriteLine("* " + menuOptions[i] + "<--");
                    }
                    else
                    {
                        Console.WriteLine(menuOptions[i]);
                    }
                }
                // 移除了所有多余的Console.ReadLine()

                // 加true参数避免按键字符显示在控制台,保持界面整洁
                var keyPressed = Console.ReadKey(true);

                if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
                {
                    menuSelect++;
                }
                else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
                {
                    menuSelect--;
                }
                else if (keyPressed.Key == ConsoleKey.Enter)
                {
                    switch (menuSelect)
                    {
                        case 0:
                            Newcustomer();
                            break;
                        case 1:
                            NewStaff();
                                break;
                        case 2:
                            Service();
                            break;
                        case 3:
                            Reparation();
                                break;
                        case 4:
                            Garantie();
                            break;
                    }
                }
            }
        }
        public static void Newcustomer(){
            Console.WriteLine("You have chosen to registrate a new customer!");
            Console.WriteLine("Please enter name of customer:");
            string name = Console.ReadLine();
            Console.WriteLine("Enter car brand:");
            string carBrand = Console.ReadLine();
            Console.WriteLine("Enter model of car");
            string model = Console.ReadLine();
            Console.WriteLine("Enter year model");
            string yearModel = Console.ReadLine();
            Console.WriteLine("Enter how many km the car has been driven");
            string km = Console.ReadLine();
            Console.WriteLine("Registration compleated!");
            Console.WriteLine("You have registered a new customer! You have registrered" + " " + name + " " + "with the carbrand" + carBrand + " " + "of model" + " " + model + " " + "form year" + " " + yearModel + " " + "That has been driven" + " " + km + "km.");
            Console.ReadKey();
        }
        public static void NewStaff(){
            Console.WriteLine("You have chosen to registrer a new staffmember!");
            Console.WriteLine("Please enter name of staffmember:");
            string staffName = Console.ReadLine();
            Console.WriteLine("Enter age:");
            string ageStaff = Console.ReadLine();
            Console.WriteLine("Enter work position of the new staffmember:");
            string position = Console.ReadLine();
            Console.WriteLine("Enter type of contract:");
            string contract = Console.ReadLine();
            Console.WriteLine("Enter date of the workers first day:");
            string start = Console.ReadLine();
            Console.WriteLine("Enter date of the workrs last day:");
            string end = Console.ReadLine();
            Console.WriteLine("Registration compleated!");
            Console.WriteLine("You have registrered a new staffmember! The following information has been registered:\t" + "name: " + staffName + "\t" + "age: " + ageStaff + "\t" + "position: " + position + "\t" + "Contract: " + contract + "\t" + "First day: " + start + "\t" + "Last day: " + end);
            Console.ReadKey();
        }
        public static void Service(){
            Console.WriteLine("Please enter type of service:");
            string service = Console.ReadLine();
            Console.WriteLine("Enter the name of an registered customer. If customer is not already registered, please press Enter to return to menu and choose `New customer`");
            string customerservice = Console.ReadLine();
            Console.WriteLine("Enter staffmember responsible for this matter. If staffmember is new, please press Enter to return to the menu and choose `New staff`");
            string servicestaff = Console.ReadLine();
            Console.WriteLine("Enter staring day of service:");
            string serviceStart = Console.ReadLine();
            Console.WriteLine("Enter deadline of servicematter:");
            string deadline = Console.ReadLine();
            Console.WriteLine("Service matter registrered!");
            Console.WriteLine("Following information has been registrered:\t" + "Service matter: " + service + "\t" + "Customer: " + customerservice + "\t" + "Staff member responsible: " + servicestaff + "\t" + "Startdate of service matter: " + serviceStart + "\t" + "Deadline: " + deadline);
            Console.ReadKey();
        }
        public static void Reparation(){
            Console.WriteLine("You have choosen to registrer a reparation matter!");
            Console.ReadLine();
            Console.ReadKey();
        }
        public static void Garantie(){
            Console.WriteLine("You have choosen to registrate a garantie matter!");
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

内容的提问来源于stack exchange,提问作者JohannaH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 23:27:05