2017-03-02 130 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