首页 > 解决方案 > 切换到默认大小写

问题描述

我不明白为什么我的开关盒不起作用。我试图从转换为 Int32 的 Console.Read 变量中获取用户输入,但开关直接跳转到默认情况。

主要方法:

    class Program {
    static void Main(string[] args) {
        Console.WriteLine("##############################################");
        Console.WriteLine("#                                            #");
        Console.WriteLine("#     1 - ADD ITEM       3 - VIEW ITEMS      #");
        Console.WriteLine("#     2 - EDIT ITEM      4 - DELETE ITEM     #");
        Console.WriteLine("#                                            #");
        Console.WriteLine("##############################################");

        // Get input from user
        int userChoice = Convert.ToInt32(Console.Read());

        // Call function that responds to user selection from main menu
        Functions function = new Functions();
        function.SelectFunction(userChoice);
    }
}

这是从一个名为 Functions 的类中获取的 switch 方法:

public void SelectFunction(int choice) {
        switch (choice) {
            case 1:
                Console.WriteLine("ADD ITEM");
                /*Console.WriteLine("Please enter ID:");
                var id = Console.ReadLine();
                Console.Write("Item name: ");
                var name = Console.ReadLine();*/
                break;
            case 2:
                Console.WriteLine("EDIT ITEM");
                break;
            case 3:
                Console.WriteLine("VIEW ITEMS");
                break;
            case 4:
                Console.WriteLine("DELETE ITEM");
                break;
            default:
                Console.WriteLine("Input not found in any of the choices. Please try again.");
                Console.ReadKey();
                break;
        }
    }

标签: c#switch-statement

解决方案


推荐阅读