After writing for one night, my knees and above are all paralyzed;
Next, select structure 2 (syntax ):
Swith (expression ){
Case constant 1:
// Code block 1;
Break;
Case constant 2:
// Code block 2;
Break;
.........
Default:
// Code block N;
Break;
}
Switch: equivalent judgment (only int and char can be judged). When multiple branches are required and the condition judgment is equivalent, the switch structure is simpler.
Of course, when using the switch structure, do not forget to add the break statement after each case.
Switch: indicates "Switch ". This switch is the value of the parentheses following the switch keyword. An integer variable (expression) or variable (expression) must be placed in the parentheses );
Case: It indicates "situation, situation". After case, it must be an integer or complex type constant, usually a fixed character, number, such as 8, 'A '. There can be multiple case blocks and the order can be changed, but the values of constants after each case must be different.
Default: indicates "default", that is, none of the above conditions is met. Followed by the colon. The order of defaule blocks and case blocks can be changed without affecting the execution result of the program. Generally, the default block is placed at the end or omitted.
Break: indicates "stop", that is, the current structure is exceeded.
If you want to enter a number for fear of entering characters, the system prompts you:
Input. hasnext
Example: int type is required,
Add an IF (input. hasnextint () = true ){
This line is accepted from the console
Then switch structure
} Else {
System. Out. println ("your input is incorrect. Please enter the correct number ");
}
No. 4