简体   繁体   English

C#Switch语句重复吗?

[英]C# Switch statement duplicating?

Im trying to use a switch to allow a choice for the user to make. 我试图使用一个开关来允许用户做出选择。 However when the default in the switch is executed, it will print the WriteLine from "room3" 1 more time than the default got executed for example. 但是,当执行开关中的默认值时,它将比例如执行默认值多1次从“ room3”打印WriteLine。 Default gets executed 2 times, The WriteLine in "room3" gets executed 3 times. 默认执行2次,“ room3”中的WriteLine执行3次。

Im just trying to make a simple Pick your own adventure game for a class at my school and I need help figuring this one out. 我只是想做一个简单的选择自己的冒险游戏在学校上课,我需要帮助弄清楚这个游戏。 Im also pretty new to c# so 我对C#来说也很新

Thank you for you help in advance! 谢谢您的提前帮助!

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 calls sword, which (in the case of default) calls swordChoice, which calls sword, and so on... It's a recursive loop, which, when unwinding, calls room3 for each time the loop recursed. swordChoice调用剑,(在默认情况下)调用wordsChoice,调用剑,依此类推...等等。这是一个递归循环,展开时,每次循环递归时都调用room3。 Change the break statement in the default clause to return instead of break, and your problem will go away. 将default子句中的break语句更改为return而不是break,您的问题将消失。

I know the question is already answered, but thought this might help you better visualize what's going on and this won't fit into a comment. 我知道问题已经得到回答,但是认为这可以帮助您更好地可视化正在发生的情况,因此不适合发表评论。 For simple programs like this it doesn't hurt to get paper and pencil and draw the execution structure out. 对于像这样的简单程序,拿纸笔和画出执行结构并没有什么坏处。

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

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

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