2017-03-02 139 views
0

我試圖用一個開關來允許用戶做出選擇。但是,如果執行交換機中的默認設置,則它將從「room3」打印WriteLine比默認執行的時間多1次。默認執行2次,「room3」中的WriteLine被執行3次。C#切換語句重複?

我只是想做一個簡單在我的學校挑選你自己的冒險遊戲,我需要幫助找出這一個。我也很新的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 
    } 
+1

用調試程序遍歷代碼,看看它在做什麼。 – Servy

+1

'room3();'執行的次數與'swordChoice'的執行次數一樣多,而不是'default'次數的執行次數 – UnholySheep

+7

今天是你學習如何調試小程序而不要求互聯網爲你做的一天;這是一項將持續一生的寶貴技能。 :-) https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

回答

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

swordChoice呼籲劍,這(在默認的情況下)調用swordChoice,它調用劍,等等...這是一個遞歸循環,其中,平倉的時候,每一個循環遞歸時間要求ROOM3 。更改默認子句中的break語句以返回而不是中斷,並且問題將消失。

+0

你的權利非常感謝你,這對理解switch語句的工作原理有很大的幫助! – Josh