|
SwitchDemo |
|
1 // Example 5.1 joi/examples/SwitchDemo.java 2 // 3 // 4 // Copyright 2003 Bill Campbell and Ethan Bolker 5 6 // A class illustrating the Switch statement 7 8 // %> java SwitchDemo 9 // Enter an integer: 2 10 // two 11 // Notice the importance of the breaks! 12 // The same statement without the breaks: 13 // two 14 // three 15 // Not one, two or three! 16 // Enter a character: y 17 // yes 18 // %> 19 20 public class SwitchDemo 21 { 22 public static void main( String[] args ) 23 { 24 Terminal terminal = new Terminal(); 25 26 int i = terminal.readInt( "Enter an integer: " ); 27 28 switch (i) { 29 case 1: 30 terminal.println( "one" ); 31 break; 32 case 2: 33 terminal.println( "two" ); 34 break; 35 case 3: 36 terminal.println( "three" ); 37 break; 38 default: 39 terminal.println( "Not one, two or three!" ); 40 } 41 42 terminal.println( "Notice the importance of the breaks!" ); 43 terminal.println( "The same statement without the breaks:" ); 44 45 switch (i) { 46 case 1: 47 terminal.println( "one" ); 48 case 2: 49 terminal.println( "two" ); 50 case 3: 51 terminal.println( "three" ); 52 default: 53 terminal.println( "Not one, two or three!" ); 54 } 55 56 switch (terminal.readChar( "Enter a character: " )) { 57 case 'y': 58 terminal.println( "yes" ); 59 break; 60 case 'n': 61 terminal.println( "no" ); 62 break; 63 default: 64 terminal.println( "Neither yes nor no." ); 65 } 66 } 67 } 68
|
SwitchDemo |
|