Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 as a capital letter. However, I do want the program to accept both lower- and upper-case characters.

I understand that switch statements are used to compare against constants, but is it possible to do something like the following?

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

If this isn't possible, is there a workaround? I really don't want to repeat code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

This way:

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

More on that topic: http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...