C#Switch语句重复?

时间:2017-03-02 21:41:43

标签: c# switch-statement

我试图使用开关来允许用户做出选择。但是,当执行开关中的默认值时,它将从“room3”打印WriteLine 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
    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我知道问题已经得到解答,但我认为这可能有助于您更好地直观地了解正在发生的事情,这不符合评论。对于像这样的简单程序,获取纸张和铅笔以及绘制执行结构并没有什么坏处。

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