Java Reference
In-Depth Information
Display 3.2
A switch Statement
(part 1 of 2)
1
import
java.util.Scanner;
2
3
public class
SwitchDemo
4 {
5
public static void
main(String[] args)
6 {
7 Scanner keyboard =
new
Scanner(System.in);
8 System.out.println("Enter number of ice cream flavors:");
9
int
numberOfFlavors = keyboard.nextInt( );
10
switch
(numberOfFlavors)
11 {
12
case
32:
13 System.out.println("Nice selection.");
14
break
;
15
case
1:
16 System.out.println("I bet it's vanilla.");
17
break
;
18
case
2:
19
case
3:
20
case
4:
21 System.out.println(numberOfFlavors + "flavors");
22 System.out.println("is acceptable.");
23
break
;
24
default
:
25 System.out.println("I didn't plan for");
26 System.out.println(numberOfFlavors + " flavors.");
27
break
;
28 }
29 }
30 }
Controlling expression
Case labels
break
statement
Sample Dialogue 1
Enter number of ice cream flavors:
1
I bet it's vanilla.
Sample Dialogue 2
Enter number of ice cream flavors:
32
Nice selection.