在C#中循环switch语句有奇怪的错误

时间:2014-06-04 13:42:01

标签: c# while-loop switch-statement fall-through

我最近在学习C#,但我遇到了一个问题。

public static void PlayGame(Kion Player1,Kion Player2)
    {
        bool Win = false;
        Console.WriteLine ("Let us begin then");
        Console.WriteLine ("Press Enter to roll the dice ");
        Console.ReadLine ();
        Console.WriteLine ();
        Console.WriteLine ();
        Random Dice = new Random();
        while (Win == false)
        {
            int DiceResult = Dice.Next (1,6);

            switch(DiceResult)   ***control cannot fall-through from one case label to another error message here***
            {
            case 1:
            case 4:
                Console.WriteLine ("The attribute being played is Strength");
                if ((Player1.Pride == "Kan") & (Player2.Pride == "Kan"))
                    Console.WriteLine ("You are both proud Kans, you match in combat and do not lose health");
                else
                    Console.WriteLine ("Those who belong to the 'Kan' pride unleashes their claws");
                    if (Player1.Pride == "Kan")
                {
                    Player2.LoseHealth();
                    int PlayerNumber = 2;
                    LoseHealthText(PlayerNumber, Player2);
                }
                    else
                    if (Player2.Pride == "Kan")
                {
                    Player1.LoseHealth ();
                    int PlayerNumber = 1;
                    LoseHealthText(PlayerNumber, Player1);
                }
                    else 
                    Console.WriteLine ("None belong to the Kan, you will all hide this turn");
                break;

            case 3:
            case 6:
                Console.WriteLine ("hello");
            }
        }
    }

上面的代码无法执行,因为编译器在有control cannot fall-through from case label to another语句的行上报告错误switch(DiceResult)

有人可以帮我确定我的错误吗?

2 个答案:

答案 0 :(得分:0)

尝试在

之后休息一下
Console.WriteLine ("The attribute being played is Strength");

所以你有:

 switch(DiceResult) // **control cannot fall through case on this line**
            {
            case 1:
            case 4:
                Console.WriteLine ("The attribute being played is Strength");
            break;
            }

答案 1 :(得分:0)

case 3:
case 6:
    Console.WriteLine ("hello");

在这里你需要一个break;语句,如下所示:

case 3:
case 6:
    Console.WriteLine ("hello");
    break;

C#中,您无法在没有casebreak;return;声明的情况下创建throw选项。 Please refer MSDN|了解更多信息。