【问题标题】:Choosing in menu wirh arrows on keyboard (C#)在带有键盘上箭头的菜单中选择 (C#)
【发布时间】:2021-11-04 15:33:39
【问题描述】:

我正在尝试制作一个简单的菜单,我可以在其中使用键盘上的箭头进行选择。我可以运行代码并且程序没有给出任何错误,但是我不能用箭头进行选择,如果我按 Enter 没有任何反应,选择始终保持在第一选择(新客户)上。那就是说,它总是这样:

您好,欢迎您!请选择注册类型:

*新客户

新员工

服务

赔偿

保证

到目前为止我的代码是:

using System;

namespace uppdrag_2.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Serivce\t", "Reparation", "Garantie" };
            int menuSelect = 0;

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

                if (menuSelect == 0)
                {
                    Console.WriteLine("* " + menuOptions[0] + "<--");
                    Console.WriteLine(menuOptions[1]);
                    Console.WriteLine(menuOptions[2]);
                    Console.WriteLine(menuOptions[3]);
                    Console.WriteLine(menuOptions[4]);
                    Console.ReadLine();
                }
                else if (menuSelect == 1)
                {
                    Console.WriteLine(menuOptions[0]);
                    Console.WriteLine("* " + menuOptions[1] + "<--");
                    Console.WriteLine(menuOptions[2]);
                    Console.WriteLine(menuOptions[3]);
                    Console.WriteLine(menuOptions[4]);
                    Console.ReadLine();
                }
                else if (menuSelect == 2)
                {
                    Console.WriteLine(menuOptions[0]);
                    Console.WriteLine(menuOptions[1]);
                    Console.WriteLine("* " + menuOptions[2] + "<--");
                    Console.WriteLine(menuOptions[3]);
                    Console.WriteLine(menuOptions[4]);
                    Console.ReadLine();
                }
                else if (menuSelect == 3)
                {
                    Console.WriteLine(menuOptions[0]);
                    Console.WriteLine(menuOptions[1]);
                    Console.WriteLine(menuOptions[2]);
                    Console.WriteLine("* " + menuOptions[3] + "<--");
                    Console.WriteLine(menuOptions[4]);
                    Console.ReadLine();
                }
                else if (menuSelect == 4)
                {
                    Console.WriteLine(menuOptions[0]);
                    Console.WriteLine(menuOptions[1]);
                    Console.WriteLine(menuOptions[2]);
                    Console.WriteLine(menuOptions[3]);
                    Console.WriteLine("* " + menuOptions[4] + "<--");
                    Console.ReadLine();
                }
                var keyPressed = Console.ReadKey();

                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();
        }
    }
}

谁能帮我弄清楚我做错了什么?

【问题讨论】:

  • 编辑您的问题并修复代码格式,然后添加问题所涉及的编程语言的标签(Java?),否则没人会找到它。
  • 好的!我现在写的是 C# im writing in, but what do you mean by "fix the code formatting" (sorry Im 对编程来说非常新)
  • 在编写每组菜单选项后,您有Console.ReadLine() - 这会在等待输入时阻止执行。你有这些Console.ReadLine() 电话有什么原因吗?
  • 好的!我删除了它们,现在它可以工作了,谢谢!
  • 我不知道为什么我把它们写在那里哈哈

标签: c# console-application arrows


【解决方案1】:

您可以通过使用for 循环和ternary if statement 来大大缩短菜单代码:

    static void Main(string[] args)
    {
        string[] menuOptions = new string[] { "New customer\t", "New staff\t", "Serivce\t", "Reparation\t", "Garantie\t" };
        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++)
            {
                Console.WriteLine((i == menuSelect ? "* " : "") + menuOptions[i] + (i == menuSelect ? "<--" : ""));
            }

            var keyPressed = Console.ReadKey();

            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("New Customer ...");
        Console.Write("Press Enter to Continue");
        Console.ReadLine();
    }

    public static void NewStaff()
    {
        Console.WriteLine("New Staff ...");
        Console.Write("Press Enter to Continue");
        Console.ReadLine();
    }

    public static void Service()
    {
        Console.WriteLine("Service ...");
        Console.Write("Press Enter to Continue");
        Console.ReadLine();
    }

    public static void Reparation()
    {
        Console.WriteLine("Reparation ...");
        Console.Write("Press Enter to Continue");
        Console.ReadLine();
    }

    public static void Garantie()
    {
        Console.WriteLine("Garantie ...");
        Console.Write("Press Enter to Continue");
        Console.ReadLine();
    }

}

对我来说很好用:

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多