C#开关语句复制?

C#开关语句复制?,c#,switch-statement,C#,Switch Statement,我试图使用一个开关,让用户做出选择。但是,当开关中的默认值被执行时,它将从“room3”1打印写线的时间比执行默认值的时间长,例如。默认值执行2次,“room3”中的WriteLine执行3次 我只是想做一个简单的选择你自己的冒险游戏在我的学校班级,我需要帮助找出这一个。我对c#so也很陌生 提前感谢您的帮助 public static void sword() { Console.WriteLine ("The lights turn on and

我试图使用一个开关,让用户做出选择。但是,当开关中的默认值被执行时,它将从“room3”1打印写线的时间比执行默认值的时间长,例如。默认值执行2次,“room3”中的WriteLine执行3次

我只是想做一个简单的选择你自己的冒险游戏在我的学校班级,我需要帮助找出这一个。我对c#so也很陌生

提前感谢您的帮助

public static void sword()
    {           
        Console.WriteLine ("The lights turn on and your in a similar room to your " +
        "cell just alot bigger. What would you like to do?");
        Console.WriteLine ("1) Look around");
        Console.WriteLine ("2) Kick something");
        Console.WriteLine ("3) Go back towards your cell");
        swordChoice ();
    }

public static void swordChoice ()
    {

        string userValue = Console.ReadLine ();

        //Broken because when the default comes up it 
        //will print the “room3” line multiple times.
        switch (userValue) {    
        case "1":

            Console.WriteLine ("You start looking around but theres not much to see.");
            Console.ReadLine ();
            Console.WriteLine ("You start heading back towards your cell.");
            Console.ReadLine ();

            break;
        case "2":

            Console.WriteLine ("Thats pointless get your head in the game.");
            Console.ReadLine ();

            goto default;
        case "3":

            Console.WriteLine ("You start heading back towards your cell.");
            Console.ReadLine ();

            break;
        default:

            Console.WriteLine ("Well you cant do nothing, Please choose 1, 2 or 3");

            swordChoice ();
            break;
        }

            room3 ();
    }

    public static void room3 ()
    {
        Console.WriteLine ("You made it back.");
        Console.ReadLine ();
        //More dialouge here
    }

swordChoice调用sword,sword(默认情况下)调用swordChoice,sword调用sword,依此类推。。。这是一个递归循环,在展开时,每次循环递归时都调用room3。将default子句中的break语句更改为return而不是break,您的问题就会消失。

我知道问题已经得到了回答,但我认为这可能有助于您更好地可视化正在发生的事情,而这不适合添加到注释中。对于像这样的简单程序,用纸和铅笔画出执行结构并没有什么坏处

1. Sword() is called
2. Console.WriteLine(...) is called multiple times to display options.
3. swordChoice() is called.
   \\within swordChoice()
   4. Console.ReadLine() is called to retrieve users answer.
      (Undesired input so it falls to the default case) 
   5. Console.WriteLine() is called.
   6. swordChoice() is called.
      \\within swordChoice() #2
      7. Console.ReadLine() is called to retrieve users answer.
       (Assuming a desired input is entered. Case 3, just because. )
      8. Console.WriteLine();
      9. Console.ReadLine();
        (breaks from the statement in case "3")
      10. room3() is called the first time.
          \\within room3()
          11. Console.WriteLine ("You made it back.");
          12. Console.ReadLine ();
          \\function is completed so it returns to where it was called, which was just before the break in the default case
   (breaks from the statement in the default case)
   13. room3() is called the second time.
       \\within room3() #2
       14. Console.WriteLine ("You made it back.");
       15. Console.ReadLine ();

使用调试器逐步检查代码,查看它在做什么的执行频率与
swordChoice
的执行频率相同,而不是与
default
的执行频率相同。今天,您可以学习如何调试小程序,而不必要求internet为您调试;这是一项宝贵的终身技能。:-)阅读博客并尝试单步思考代码,但两者似乎都不能解决问题:\n单步执行代码并不能解决问题。提出一个关于代码应该做什么的假设,然后观察它实际上做了什么,然后找出两者不同的原因,然后找出如何修改代码来做它应该做的事情,这就是解决问题的方法。您的权利非常感谢您,这对理解switch语句的工作方式是一个巨大的帮助!