/*
Switch statement
switch (expression) {
Case value 1:
Statement body 1;
Break
Case Value 2:
Statement body 2;
Break
..
..
Default
Statement body n+1;
Break
}
Format interpretation of B:switch statements
(1): switch indicates that this is a switch statement
Value of expression: Byte,short,int,char
JDK5 can be enumerated later (what is an enumeration, again later)
JDK7 can be a string later
(2): The case is followed by the value to be compared with the expression
(3): The statement body part can be one or more statements
(4): Break indicates interrupt, end of meaning, can end switch statement
(5):d The Efault statement indicates that all cases do not match, the contents of that place are executed, and the else of the IF statement is similar
C: Face question
Can byte be used as a switch expression? OK
Can long be used as a switch expression? No
Can string be used as an expression of switch? Yes, after the JDK7.
D: Execution Process:
The value of an expression is evaluated first:
Match the value in the case.
If a successful match succeeds, the statement that matches the value below is encountered when the break jumps out, otherwise the default control is executed
A: Integers (two-digit plus-minus multiplier)
There are two integers
Keyboard entry gets two integers
I have a word for you to choose from.
1: Perform an addition of two numbers
2: Perform a subtraction of two numbers
3: Perform a multiplication of two numbers
4: Perform a division of two numbers
*/
Guide Package
Import Java.util.scanner;class switchdemo{public static void Main (string[] args) {//create object Scanner sc = new Scanner (system.i n); System.out.println ("Please enter the first integer!"); int x = Sc.nextint (); System.out.println ("Please enter a second integer!"); int y = Sc.nextint (); SYSTEM.OUT.PRINTLN ("Please select the action you want to do:!"); System.out.println ("1: Addition 2: Subtraction 3: Multiplication 4: Division"); int choose = Sc.nextint (); switch (choose) {case 1:system.out.println ("sum of two numbers is:" + (x+y)); Break Case 2:system.out.println ("The difference between two numbers is:" + (x-y)); Break Case 3:SYSTEM.OUT.PRINTLN ("the product of two numbers is:" + (x*y)); Break Case 4:system.out.println ("quotient of two numbers is:" + (x/y)); Break DEFAULT:SYSTEM.OUT.PRINTLN ("Not supported for the time being, I will give you support!!"); }}}
/*
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.
Can c:break be omitted? The program runs out of a break and does not encounter a break down execution until a break is encountered or the program finishes executing
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.
Because the program execution process is a case-by-case judgment case all values do not match to execute the statement in default
The end condition of the E:switch statement
A: It's over when you hit the break.
B: Execution ends at the end.
*/
Guide Package
Import java.util.scanner;class switchnoticedemo{public static void main (String[] args) {//Create Object Scanner sc = new scanner (system.in); System.out.println ("Please enter the first integer!"); Int x = sc.nextint (); System.out.println ("Please enter a second integer!"); Int y = sc.nextint (); system.out.println (" Please select the action you want to do:! "); System.out.println ("1: addition 2: Subtraction 3: Multiplication 4: Division"); int choose = sc.nextint (); //int a = 2;switch (choose) { case 1:system.out.println ("sum of two numbers is:" + (x+y)); break;/* case a:// requires constant expression System.out.println ("the difference between two numbers is:" + (x-y)); break; case 2://case Label Repeat System.out.println ("Two number of JKHJKL is:" + (x-y)); break; */ case 2:system.out.println ("The difference between two numbers is:" + (x-y)); break;       CASE 3:SYSTEM.OUT.PRINTLN ("The product of two numbers is:" + (X*y)); break; case 4:system.out.println ("Two number of the quotient is:" + (x/y)); break;default:system.out.println ("Not supported for the time being, give you support!!!"); }}}
This article from the "Clear Sky" blog, declined reprint!
JAVA Switch statement