Java Fundamentals (Chapter four)
The syntax of a switch structure (switching statement)
switch (expression) {-------[dream1] type is int, char
CASE constant 1:---------[dream2]case structure can have multiple
Statement Block 1
break;---------[DREAM3] program jumps out of the switch structure
Case constant N: The value of the---------[DREAM4] constant cannot be the same
Statement block N
Break
Default: Same as else in----------[DREAM5] and if structures
Statement block
Break
}
Second, switch structure use occasions: int \char 2 types of equivalence judgment to use
Iii. the similarities and differences between switch structure and if structure
Same point: equal judgment can be achieved
Different points:
L syntax is different
L use different occasions.
n if structure, focusing on range judgment
N-switch structure, focusing on equivalence judgments, types can only be int, char type
L Efficiency
N in the equivalence judgment, the switch structure efficiency is higher than the IF structure
Iv. exception handling--hasnextint ();
Determines whether an integer
if (Input.hasnextint ()) {//Boolean type
code block
}
Java Fundamentals (Chapter four)