- /**
- * IfElseIf Demo:
- * Uses command line
input to input an integer. Outputs whether the
- * integer is a 1, 2, 3, or a
default message first using an if-else-if
- * type menu and next
using a switch.
- *
- * @author (TK Rogers)
- * @version (11-26-07)
- */
-
- public class IfElseIf {
- public static void
main ( String args [] ) {
-
int test = 0;
-
int x = Integer.parseInt ( args [0] );
// converts String to int
-
- if ( x == ++test
) {
-
System.out.println ( "The input was " + test );
- } else {
-
- if ( x == ++test
) {
-
System.out.println ( "The input was " + test );
- } else {
-
- if ( x == ++test
) {
-
System.out.println( "The input was " + test );
- } else {
-
-
System.out.println( "The input was not 1,2, or 3" );
// default
- }
// closing bracket for 3rd else statement
- }
// closing bracket for 2nd else statement
- }
// closing bracket for 1st else statement
- ////////////////////////////////////////////////////////////////////////////////////////////////
- switch (x)
- {
-
case 1: {
-
System.out.println ( "The input was " + 1 );
-
break;
-
}
-
case 2: {
-
System.out.println ( "The input was " + 2 );
-
break;
-
}
-
case 3: {
-
System.out.println ( "The input was " + 3 );
-
break;
-
}
-
default: System.out.println ( "The input was not 1, 2, or 3" );
-
break;
- }
- }
- }