import java.util.Scanner; // Many examples of how selection works. // Ferworn Oct 02. revised Oct 05 public class Selection{ public static void main(String[] args){ int num; int counter = 1; Scanner Keyboard = new Scanner(System.in); System.out.println("Enter a number greater than 5."); num = Keyboard.nextInt(); // Demo 1: a simple if with a single statement following System.out.println("\n==Entering Demo " + counter++); if(num < 5) System.out.println("That was incorrect"); // Demo 2: a simple if with a block of statements following System.out.println("\n==Entering Demo " + counter++); if(num < 5) { System.out.println("That was incorrect"); System.out.println("Your input was " + num); } // Demo 3: a common mistake: forgetting the need for blocks System.out.println("\n==Entering Demo " + counter++); if(num > 5) System.out.println("Your number was " + num); System.out.println("It is greater than 5"); // Demo 4: a simple if with an else clause System.out.println("\n==Entering Demo " + counter++); if(num > 5) System.out.println(num + " is greater than 5"); else System.out.println(num + " is less than 5"); // Demo 5: a simple if with an else clause and blocks System.out.println("\n==Entering Demo " + counter++); if(num > 5) { System.out.print (num); System.out.print (" is greater than 5"); } else System.out.print (num); System.out.print (" is less than 5"); // Demo 5A: same demo rewritten for "efficiency" System.out.println("\n==Entering Demo " + counter++); System.out.print(num + " is "); if(num > 5) System.out.print("greater "); else System.out.print("less "); System.out.println("than 5"); // Demo 6: of nested ifs pick a number between 0 and 10 System.out.println("\n==Entering Demo " + counter); if(num == 5) System.out.println("The number is 5."); else if(num > 5) if(num < 7) System.out.println("Number between 5 and 7."); else System.out.println("Number 7 or greater."); else if(num > 3) System.out.println("Number between 3 and 5"); else System.out.println("Number is 3 or less."); //Demo 6A: Same demo of nested ifs with blocks System.out.println("\n==Entering Demo " + counter++ + "A"); if(num == 5) { System.out.println("The number is 5."); } else if(num > 5){ if(num < 7){ System.out.println("Number between 5 and 7."); } else{ System.out.println("Number 7 or greater."); } } else{ if(num > 3){ System.out.println("Number between 3 and 5"); } else{ System.out.println("Number is 3 or less."); } } // Demo 7: Switch (case) example. If the number is 0 or 1 a single // message will be printed if num is 2 to 5 the message for the number // it is and all the number messages below it will be printed. // default: is executed only if none of the others is. System.out.println("\n==Entering Demo " + counter); switch (num){ case 0: System.out.println("The number was 0"); break; case 1: System.out.println("The number was 1"); break; case 2: System.out.println("The number was 2"); case 3: System.out.println("The number was 3"); case 4: System.out.println("The number was 4"); case 5: System.out.println("The number was 5"); default: System.out.println ("The number was greater than 5"); } // Demo 7A: Recreate Demo 6 and make it more obvious using switch. System.out.println("\n==Entering Demo " + counter++ + "A"); switch (num){ case 0: case 1: case 2: case 3: System.out.println("Number is 3 or less"); break; case 4: System.out.println("Number between 3 and 5"); break; case 5: System.out.println("Number is 5"); break; case 6: System.out.println("Number between 5 and 7"); break; default: System.out.println("The number was 7 or greater."); } // Demo 8: true and false in java. System.out.println("\n==Entering Demo " + counter++); System.out.println("5 < 3? " + (5<3)); System.out.println("5 > 3? " + (5>3)); System.out.println("3 == 3? " + (3==3)); System.out.println("5 == 3? " + (5==3)); System.out.println("5 != 3? " + (5!=3)); System.out.println("5 <= 3? " + (5<=3)); System.out.println("5 >= 3? " + (5>=3)); System.out.println("3 >= 3? " + (3>=3)); System.out.println("5 > 3? " + (5>3)); System.out.println("num == 0? " + (num == 0)); // Demo 9: Using logic in java System.out.println("\n==Entering Demo " + counter++); int x = 77; System.out.println("Is x > 76 but less than 78? " + ((x>76) && (x<78))); System.out.println("The opposite of true is " + !true); System.out.println("I'm true if x is 77 or 78 " + ((x==77) || (x==78))); // a wierd one System.out. println("This statement prints " + (++x == 78)); }}