簡體   English   中英

C#Switch語句重復嗎?

[英]C# Switch statement duplicating?

我試圖使用一個開關來允許用戶做出選擇。 但是,當執行開關中的默認值時,它將比例如執行默認值多1次從“ room3”打印WriteLine。 默認執行2次,“ room3”中的WriteLine執行3次。

我只是想做一個簡單的選擇自己的冒險游戲在學校上課,我需要幫助弄清楚這個游戲。 我對C#來說也很新

謝謝您的提前幫助!

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調用劍,(在默認情況下)調用wordsChoice,調用劍,依此類推...等等。這是一個遞歸循環,展開時,每次循環遞歸時都調用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 ();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM