public class reprompt { // Prompt the user to enter an integer between $min$ and $max$: private static int promptForInt(int min, int max) { // Prompt for the user: String prompt = "Enter a value between "+min+" and "+max+": "; // Get initial input: System.out.print(prompt); int userValue = SavitchIn.readInt(); // Ensure the that input is legal: while(userValue < min || userValue > max) { // Explain why the input is illegal: if (userValue < min) System.out.println("Too low!"); else if (userValue > max) System.out.println("Too high!"); else { System.out.println("Something went horribly wrong!"); System.exit(0); } // Prompt the user again and test next input: System.out.print(prompt); userValue = SavitchIn.readInt(); } // Now the input is OK, so return it: return userValue; } // method promptForInt // Run some test cases: public static void main(String[] args) { System.out.println("Test 1:"); int x = promptForInt(0,10); System.out.println("Congratulations! You entered " + x + "."); System.out.println("Test 2:"); int low = 0, high = 10; System.out.println("Congratulations! You entered " + promptForInt(low,high) + "."); } // method main } // class reprompt