The value of the variable , the same as the value after the case, executes The statement after it, and encounters a break to Jump out of the switch structure.
if Case any value does not perform the same as the contents of a variable default after the statement ;
Break can be omitted, not recommended
Break
- Interrupted
- Break
- Jump out of the loop
|
If, if.. else ... |
Switch |
|
More suitable for interval range judgment: From ... to the ... |
More practical and equivalent comparison |
|
If,if...else ... must be able to understand SWITC |
Switch does not necessarily replace if,if...else ...
ImportJava.util.Scanner; Public classdome1{ Public Static voidMain (String []args) {Scanner input=NewScanner (system.in); intDay =0; System.out.print ("Please enter your score:"); Day=Input.nextint (); if(day>100| | Day <0) {System.out.print ("Input score Error"); } Else{ Day=day/10; Switch(day) { Case10:system.out.print ("A"); Break; Case9:system.out.print ("B"); Break; Case8:system.out.print ("C"); Break; Case7:system.out.print ("D"); Break; Case6:system.out.print ("E"); Break; default: System.out.print ("Failed"); } } }}
|
|
... ...
if (day = = 1) System.out.println ("Week 1");
else if (day = = 2) System.out.println ("Week 2");
else if (day = = 3) System.out.println ("Week 3");
else if (day = = 4) System.out.println ("Week 4");
else if (day = = 5) System.out.println ("Week 5");
else if (day = = 6) System.out.println ("Week 6");
else if (day = = 7) System.out.println ("Sunday");
else System.out.println ("Input Error! ”);
... ... |
... ...
Switch (day) {
Case 1:system.out.println (" Monday "); break;
Case 2:system.out.println (" Tuesday "); break;
Case 3:system.out.println (" Wednesday "); break;
Case 4:system.out.println (" Thursday "); break;
Case 5:system.out.println (" Friday "); break;
Case 6:system.out.println (" Saturday "); break;
Case 7:system.out.println (" Sunday "); break;
DEFAULT:SYSTEM.OUT.PRINTLN (" input Error! ");
}
... ...
|
|
|
|
|
Application of Java switch