// Bob Myers // File: switch1.java // // This is syntactically correct -- but logically wrong (missing break // statements public class switch1 { public static void main(String[] args) { int score; char grade; System.out.print("\nPlease enter test grade: "); score = MyInput.readInt(); switch (score / 10) { case 10: // score is 100 - 109 grade = 'A'; case 9: // score is 90 - 99 grade = 'A'; case 8: // score is 80 - 89 grade = 'B'; case 7: // score is 70 - 79 grade = 'C'; case 6: // score is 60 - 69 grade = 'D'; default: grade = 'F'; // anything below 60 flunks } System.out.println("\nStudent grade = " + grade); } } // a class for reading various types from the keyboard (System.in) class MyInput { public static String readString() { String string = ""; java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); try { string = bufferedReader.readLine(); } catch (java.io.IOException ex) { throw new RuntimeException(ex); } return string; } public static int readInt() { return Integer.parseInt(readString()); } public static double readDouble() { return Double.parseDouble(readString()); } // test all the methods of this class public static void main(String[] args) { System.out.println("Testing 'readString()'"); System.out.print("Input your string : "); System.out.println("Your string was '" + readString() + "'"); System.out.println("\nTesting 'readInt()'"); System.out.print("Input your int : "); System.out.println("Your int was '" + readInt() + "'"); System.out.println("\nTesting 'readDouble()'"); System.out.print("Input your double : "); System.out.println("Your double was '" + readDouble() + "'"); } }