2017-03-02 2 views
0

ユーザーが選択できるようにスイッチを使用しようとしています。しかし、スイッチのデフォルトが実行されると、たとえば、実行されたデフォルトよりも1時間遅れてWriteLineが "room3"から印刷されます。デフォルトは2回実行され、 "room3"のWriteLineは3回実行されます。C#ステートメントを複製する?

イムは単なる私の学校でクラスのための独自のアドベンチャーゲームを選んで作成しようと、私はこの1つを考え出す助けが必要です。イムも、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は剣を呼び出して、のように...それは巻き戻し、ループは再帰たびに、room3を呼び出し、再帰ループは、です(デフォルトの場合)swordChoiceを呼び出して剣を、呼び出し、 。 default句でbreak文をbreakの代わりに返すように変更すると、問題はなくなります。

+0

ご連絡ありがとうございました。これは、switch文の仕組みを理解する上で大きな助けになりました! – Josh

関連する問題