C# 程序未根据case语句生成预期结果

C# 程序未根据case语句生成预期结果,c#,switch-statement,C#,Switch Statement,我有一个控制台应用程序,等待用户输入1到4之间的数字。根据选择,case语句将打印出相应的控制台语句,或转到其他方法。当我启动程序并输入数字时,不会返回任何内容,程序将结束 如果用户选择数字1,我想打印出一行文本,然后执行名为NewEntry的程序。它甚至都没有启动那个程序 class Program { static void Main(string[] args) { //initial Prompt Console.WriteLine("*

我有一个控制台应用程序,等待用户输入1到4之间的数字。根据选择,case语句将打印出相应的控制台语句,或转到其他方法。当我启动程序并输入数字时,不会返回任何内容,程序将结束

如果用户选择数字1,我想打印出一行文本,然后执行名为NewEntry的程序。它甚至都没有启动那个程序

class Program
{
    static void Main(string[] args)
    {
        //initial Prompt

        Console.WriteLine("***Welcome to the Asset Directory***");
        Console.WriteLine("Please Select an Option");
        Console.WriteLine("1. New Entry");
        Console.WriteLine("2. Edit Entry");
        Console.WriteLine("3. Look Up");
        Console.WriteLine("4. Print Master List");

        int userInput;

        userInput = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine(userInput);

        switch (userInput)
        {
            case '1':
                Console.WriteLine("1. New Entry");
                NewEntry();
                break;
            case '2':
                Console.WriteLine("2. Edit Entry");
                break;
            case '3':
                Console.WriteLine("Look Up");
                break;
            case '4':
                Console.WriteLine("4. Print Master List");
                break;

            default:
                Console.WriteLine("Invalid Selection");
                break;

        }

    }

    static void NewEntry ()
    {
        Console.WriteLine("Enter the DSCADA Asset Information");
        Test_RTU = new DSCADA_RTU();
        Test_RTU.StationName = Console.ReadLine();
        Test_RTU.RTUmake = Console.ReadLine();
        Test_RTU.RTUtype = Console.ReadLine();
        Test_RTU.CommunicationType = Console.ReadLine();
        Test_RTU.DateInService = Console.ReadLine();
    }
}

class Test_RTU
{
    public string EDiv { get; set; } //division that owns the asset
    public string StationName { get; set; } // name of station RTU is located
    public string RTUmake {get; set;}      
    public string RTUtype { get; set; }    
    public string CommunicationType { get; set; } 
    public string DateInService { get; set; }  


}

您的开关盒应如下所示:

case 1:
    ...
case 2:
    ...
case 3:
    ...
case 4:
    ...
不是这个:

case '1':
    ...
case '2':
    ...
case '3':
    ...
case '4':
    ...

userInput
是一个
int
,因此大小写也应该是
int
文本。您正在使用的文本(例如
'1'
)是
char
文本。恰好有一个从
char
int
的隐式转换,将
'1'
转换为整数
49
'2'
转换为整数
50
,等等。由于这个隐式转换,您的代码通过了编译,但没有按预期工作。

userInput
是一个
Int32
,您的
case
值是
char
s。。。查看
userInput
。请注意,
'1'
=49≠ 1?这不应该还是默认情况吗?字符将被转换为int(没有匹配项),对吗?@Lester,无条件
控制台。WriteLine
调用是否适用于初始提示?