简体   繁体   English

嵌套switch语句无法访问内部switch case C#

[英]Nested switch statement not accessing inner switch cases c#

I am using nested switch statements in my program. 我在程序中使用嵌套的switch语句。 For some reason, the inner switch statements don't seem to work. 由于某种原因,内部switch语句似乎不起作用。

Expected Behaviour: Upon starting the program, the user is presented with 5 options; 预期的行为:启动程序后,将为用户提供5个选项。 A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT . A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT Of these options, A,B,C and D contain further switch statements. 在这些选项中,A,B,C和D包含其他switch语句。 Lets take for example case A: SUBJECTS . 让我们以案例A: SUBJECTS为例。 Once case A has been selected, the user is presented with a further 3 options; 一旦选择了案例A,将为用户提供另外3个选项。 A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS . A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS The user then selects one of these options after which some code is executed. 然后,用户选择这些选项之一,然后执行一些代码。 Lets say for example the user selects A, he will be taken through the procedure to add a subject to the subject pool. 举例来说,假设用户选择A,他将被引导完成将主题添加到主题库的过程。

Actual Behaviour: Upon starting the program, the initial prompt A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT is displayed prompting the user to select a case. 实际行为:启动程序后,出现初始提示A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT ,提示用户选择案例。 Once the user selects the case, he is shown the second prompt which is relevant to the option that has been selected. 用户选择案例后,将向他显示与已选择选项相关的第二个提示。 Eg If the user selected A at the initial prompt, the second prompt displayed is A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS . 例如,如果用户在初始提示下选择A,则显示的第二个提示是A: ADD_SUBJECTS, B: EDIT_SUBJECTS and C: DELETE_SUBJECTS Perfect, that's how I want this to work. 完美,这就是我希望它能起作用的方式。 But here's the problem. 但这就是问题所在。 When the user now selects an option, no matter what option has been selected, the program acts as if the ADD_SUBJECT case was chosen and goes through the code for that. 现在,用户选择一个选项时,无论选择了哪个选项,该程序都会像选择了ADD_SUBJECT大小写一样工作,并执行相应的代码。 That's for the parent case being A: SUBJECTS . 这是针对父案例为A: SUBJECTS As for the rest of the parent cases, the relevant child prompt is shown but any input/selection of the child case results in the initial prompt A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT being shown, it doesn't access any of the inner cases at all. 至于其余的父案例,则显示相关的子提示,但子案例的任何输入/选择都会导致初始提示A: SUBJECTS, B: STUDENTS, C: ENROLMENT, D: GRADES_RECORD E: EXIT ,它根本不访问任何内部情况。

Please refer to code below: 请参考以下代码:

   static void Main(string[] args)
        {
            Console.Title = "Students Record";
            LoadJson();
            Run();
        }

    static void Run()
    {
        while (true)
        {
            var consoleInput = ReadFromConsole(MainPrompt);
            if (string.IsNullOrWhiteSpace(consoleInput)) continue;

            Execute(consoleInput);
        }
    }

    public static string ReadFromConsole(string prompt)
    {
        Console.Write(prompt);
        return Console.ReadLine();
    }


static void Execute(string userChoice)
{        
switch(userChoice.ToUpper())
{
case A:
{
          ReadFromConsole(SubjectPrompt);

           switch (userChoice.ToUpper())
           {
              case "A"/*ADD_SUBJECT*/:
              {
                AddSubject();
                break;      
              }

               case "B"/*EDIT_SUBJECT*/:
               {
                  Console.WriteLine("edit subject");
                  break;
               }

               case "C"/*DELETE_SUBJECT*/:
               {
                   Console.WriteLine("delete subject");
                   break;
               }
         }
         break;
 }
case B:/*{some code}*/
case C:/*{some code}*/
case D:/*{some code}*/
case E:/*{some code}*/
}
}

Cases B,C and D are of the same format. 案例B,C和D的格式相同。

Can anyone see why for parent case A it always prompts to add a subject regardless of the what case is selected and for the rest of the parent cases it just displays the initial prompt? 谁能看到为什么对于父案例A,无论选择哪种情况,它总是提示添加主题,而对于其余父案例,它仅显示初始提示?

Thanks in advance 提前致谢

Method ReadFromConsole return a string but you aren't assign it to userChoice , that maintain the value it has when entering the method. 方法ReadFromConsole返回一个string但您没有将其分配给userChoice ,该string保持输入方法时它的值。

case "A":
{
    userChoice = ReadFromConsole("Subject");

    switch (userChoice.ToUpper())
        case "A"/*ADD_SUBJECT*/:
        {
            AddSubject();
            break;
        }

        case "B"/*EDIT_SUBJECT*/:
        {
            Console.WriteLine("edit subject");
            break;
        }

        case "C"/*DELETE_SUBJECT*/:
        {
            Console.WriteLine("delete subject");
            break;
        }
    }
    break;
}

The variable userChoice is still equal to "A" in the second switch statement, just erase its value or create a new variable and pass that new value to the second switch: 在第二个switch语句中,变量userChoice仍等于“ A” ,只需擦除其值或创建一个新变量,然后将该新值传递给第二个开关:

...
var newInput = ReadFromConsole(SubjectPrompt);
switch (newInput.ToUpper())
{
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM