When there are too many conditional judgment statements (if statements), you can use switch statements to write them. The basic structure of a switch statement is:
switch (integer) {
Case Integer value 1: statement; Break
Case Integer value 2: statement; Break
Case Integer value 3: statement; Break
..........................
Default: statement;
}
When the value of "integer" equals "integer value 1", "Integer value 2", "Integer value 3" ... , executes the corresponding statement, executes the completion exit Switch statement, if there is no equivalent value, executes the statement behind the default and executes the exit switch statement.
NOTE: The break statement is used to jump out of a switch statement.
Example: Output the current month.
public class control17{
public static void Main (string[] args) {
int i=8;
switch (i) {
Case 1:system.out.println ("January");
Case 2:system.out.println ("February");
Case 3:system.out.println ("March");
Case 4:system.out.println ("April");
Case 5:system.out.println ("May");
Case 6:system.out.println ("June");
Case 7:system.out.println ("July");
Case 8:system.out.println ("August");
Case 9:system.out.println ("September");
Case 10:system.out.println ("October");
Case 11:system.out.println ("November");
Case 12:system.out.println ("December");
Default:System.out.println ("Fault");
}
}
}
Operation Result:
Is August
4.Java Switch Statement-switch