简体   繁体   中英

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. Default gets executed 2 times, The WriteLine in "room3" gets executed 3 times.

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

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. Change the break statement in the default clause to return instead of break, and your problem will go away.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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