CS160: Foundations in Programming: Spring 2015
Scanner keyboard = new Scanner(System.in); System.out.print("Please enter your age: "); int age = keyboard.nextInt(); switch (age) { case 21: System.out.println("You are 21 and the same age as Miley Cyrus! Not sure if that's good or bad."); break; case 18: System.out.println("You're 18 years old! and the same age as Zendaya. No idea who she was -- I had to Google her."); break; case 20: System.out.println("You're 20 years old and the same age as Justin Bieber! Has he retired yet?"); break; case 16: System.out.println("You're 16 years old and the same age as Jaden 'Don't call me Will' Smith!"); break; default: System.out.println("Oops! You're not the age of any people I know."); break; } keyboard.close(); System.out.println("End of program.");We did the same thing in R5, but this time we used switch instead of if.. else if.. else... You can also use chars and Strings in your switch (see Part 2 for an example with a String and Part 3 for a char example).
Scanner keyboard = new Scanner(System.in); System.out.print("Please enter your first name: "); String firstName = keyboard.next(); switch (firstName) { case "Miley": System.out.println("Do you like to twerk?"); break; case "Justin": System.out.println("I have Bieber-fever."); break; case "Chris": System.out.println("You must love chai from the Alley Cat."); break; case "Arnold": System.out.println("Get to da choppa!!"); break; default: System.out.println("Sorry mate, I don't recognize your first name."); break; } keyboard.close(); System.out.println("End of program.");Notice that the format is the same; the only change is data type we use.
Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a letter: "); char myChar = keyboard.next().charAt(0); // what's going on here? switch (myChar) { case 'a': System.out.println("a!"); case 'b': System.out.println("b!"); case 'c': System.out.println("c!"); break; case 'd': System.out.println("d!"); default: System.out.println("I'm the fallback case."); } keyboard.close(); System.out.println("End of program.");What happens when you input 'a'? What happens when you input 'b'? What happens when you input 'c'? What happens when you input 'p'?
Scanner keyboard = new Scanner(System.in); // Here we print a welcome message System.out.println("Welcome to a simple calculator program!"); System.out.println("---------------------------------------"); System.out.println("All we know how to do is add, subtract, multiply, divide, and compute modulo."); System.out.println(); // Here we get input from the user System.out.print("Please enter a the first number of your calculation: "); double lhs = keyboard.nextDouble(); System.out.print("Please enter the operator (+|-|*|/|%): "); // ADD: get operator using Scanner object System.out.print("Please enter a the second number of your calculation: "); // ADD: get the second number use the Scanner object // Here we will calculate the result: double result; // ADD: write a switch statement that will calculate // the correct result. // Here we will print the result System.out.println("---------------------------------------"); System.out.println("Result:"); // ADD: print the answer to the calculation using the // so it looks like: "2.44 - 1.44 = 1.00" System.out.println("---------------------------------------"); keyboard.close(); System.out.println("End of program.");This code is not complete. Fill in the blanks to finish the program.