Switch statement is a single conditional multi-branch switch statement, its general format is defined as follows ( where The break statement is optional):
Switch ( expression )
{
Case constant Value :
a number of statements
Break
Case constant Value :
a number of statements
Break
...
Case constant Value N:
a number of statements
Break
Default
several statements
}
The values for expression and constant values in the switch statement must be byte,short,int, Char type or enum type, and not identical.
Import Java.util.Scanner;
public class demo9{
public static void Main (String args[]) {
Scanner reader = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("please input int");
int input = Reader.nextint (); /* get input int data
switch (input) {
Case 1:/* input is 1
SYSTEM.OUT.PRINTLN ("You input 1");
Break
Case 2:
SYSTEM.OUT.PRINTLN ("You input 2"); /* input is 2
Break
Default:/* input is neither 1nor 2
SYSTEM.OUT.PRINTLN ("You input Other");
Break
}
}
}
Results
Input 2
Input 1
Enter a different number
original link:http://www.maiziedu.com/wiki/java/switch/
A detailed description of the switch statement for Java Basics