switch语句中的if语句

An if statement within a switch statement?

本文关键字:语句 if switch      更新时间:2023-10-16

我很难理解为什么一种方法有效而另一种方法无效。

我有;

switch (key)
        {
            //If Game over Label is visible, enable the m and e buttons
            if(mGameOverLabel->GetVisible())
            {
                case 'm': case 'M':
                    ResetScreen();
                    break;
                case 'e': case 'E':
                //  //Exit the game
                    Stop();
                    break;
            } else {
                case ' ':
                    mSpaceship->Shoot();
                    break;
                default:
                    break;
            }

对于m和e的情况,即使mGameOverLabel在当前时间设置为false,我仍然可以按m和e,它们将根据方法做出反应,但如果我将m的值更改为此值,那么它也只能在我需要时工作。我是不是错过了什么?!

switch (key)
        {
            //If Game over Label is visible, enable the m and e buttons
            case 'm': case 'M':
                if(mGameOverLabel->GetVisible()) ResetScreen();
                break;
            }   

switch基本上对适当的事例标签执行gotocase以上的任何逻辑都不会被执行。