switch statement considerations:
A:case can only be constants, cannot be variables, and values after multiple case cannot appear the same
Can B:default be omitted?
It can be omitted, but it is not recommended, because it is used to prompt for incorrect conditions.
Special cases:
case, you can fix the value.
A,b,c,d
Can c:break be omitted?
can be omitted, but the result may not be what we want.
There is a phenomenon: case penetrating.
In the end, we recommend not omitting
D:default must be at the end?
No, it can be anywhere. But the suggestion at the end.
The end condition of the E:switch statement
A: It's over when you hit the break.
B: Execution ends at the end.
1 ImportJava.util.Scanner;2 3 classSwitchDemo2 {4 Public Static voidMain (string[] args) {5 //Creating a Keyboard entry object6Scanner sc =NewScanner (system.in);7 8 //control keyboard input data9System.out.println ("Please enter a data (1-7):");Ten intWeek = Sc.nextint ();//3 One A //Defining Constants - //int number = 3; - //then change the value of the case to number, and you will get an error. the - //Switch Judgment Statement - Switch(week) { - Case1: +System.out.println ("Monday")); - Break; + Case2: ASystem.out.println ("Tuesday")); at Break; - Case3: -System.out.println ("Wednesday")); - Break; - Case4: -System.out.println ("Thursday")); in Break; - Case5: toSystem.out.println ("Friday")); + Break; - Case6: theSystem.out.println ("Saturday")); * Break; $ Case7:Panax NotoginsengSystem.out.println ("Sunday")); - Break; the default: +SYSTEM.OUT.PRINTLN ("The data you entered is wrong"); A //Break ; the } + } -}
Java Note 9 switch