我试图用一个开关来允许用户做出选择。但是,如果执行交换机中的默认设置,则它将从“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
}
用调试程序遍历代码,看看它在做什么。 – Servy
'room3();'执行的次数与'swordChoice'的执行次数一样多,而不是'default'次数的执行次数 – UnholySheep
今天是你学习如何调试小程序而不要求互联网为你做的一天;这是一项将持续一生的宝贵技能。 :-) https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –