Format of the switch statement
Inx = 10
switch (expression) {
Case value 1;
Break
Case value 2;
Break
...
Defualt:
Statement n-1;
Break
}
Note Btye Short char String can be used as an expression
Long No.
That is, the type that can be converted to int in the base type can be used as a case expression.
1 Public classtest1_datatypeconversion {2 3 Public Static voidMain (string[] args) {4String name = "Zhang San";5String gender = "Man";6 Switch(gender) {7 CaseMen:8System.out.println ("Play games");9 Break;Ten CaseMs.: OneSystem.out.println ("Make-up"); A Break; - default: -System.out.println ("You are a shemale"); the Break; - - } - + - } + A at -}
Attention
One break the last one can be omitted, others cannot be omitted
The second default can be placed in any position but it is recommended to put it on the last side
1 Public classHello_world {2 3 Public Static voidMain (string[] args) {4 5Scanner sr =NewScanner (system.in);6 7 intmonth =sr.nextint ();8 Switch(month) {9 Case1:Ten Case2: One Case3: ASystem.out.println ("one Quarter"); - Break; - Case4: the Case5; - Case6: -System.out.println ("Two Quarter")); - Break; + - + A } at - } - -}
Java language basic selection structure of switch statement and its interpretation Tooho King