Switch statement using or

前端 未结 7 2397
时光说笑
时光说笑 2020-12-10 10:29

I\'m creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen

7条回答
  •  隐瞒了意图╮
    2020-12-10 10:54

    The generally accepted syntax for this is:

    switch(menuChoice) {
        case 'q':
        case 'Q':
            //Some code
            break;
        case 's':
        case 'S':
            //More code
            break;
        default:
            break;
    }
    

    i.e.: Due the lack of a break, program execution cascades into the next block. This is often referred to as "fall through".

    That said, you could of course simply normalise the case of the 'menuChoice' variable in this instance via toupper/tolower.

提交回复
热议问题