// NicerLoopingForever.java class NicerLoopingForever { public static void main(String[] args) { // describe the program System.out.println("This program demonstrates using a 'while' loop"); System.out.println("to loop endlessly. In this version, you can enter"); System.out.println("0 to exit."); // declare an integer variable int x; // loop forever while (true) { // read in a value for x System.out.println(); System.out.print("Enter an integer value [or 0 to exit]: "); x = readInt(); // if the user entered 0, exit the program if (x == 0) { System.exit(0); } // check if it's divisible by 2 if (x % 2 == 0) { System.out.println("This number IS divisible by 2"); } else { System.out.println("This number is NOT divisible by 2"); } } } /////////////////////////////////// // readString, readInt, readDouble /////////////////////////////////// public static String readString() { java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in); java.io.BufferedReader d = new java.io.BufferedReader(isr); try { return d.readLine(); } catch (Exception e) { return "error"; } } public static int readInt() { return Integer.parseInt(readString()); } public static double readDouble() { return Double.valueOf(readString()).doubleValue(); } }