/* * File: SixXorSeven.java * ---------------------- * This program displays the integers that * are divisible by either 6 or 7 but not both. * The lower and upper limits are defined as constants. * * Author: CS1MD3, Sept. 2008 */ import acm.program.*; public class SixXorSeven extends ConsoleProgram { public void run() { println("This program displays the integers between " + LOW_LIMIT + " and " + HIGH_LIMIT); println("that are divisible by either 6 or 7 but not both."); println(""); boolean isFirst = true; /* is this first number? */ for (int i = LOW_LIMIT; i <= HIGH_LIMIT; i++) { if (((i % 6 == 0) || (i % 7 == 0)) && ((i % 6 != 0) || (i % 7 != 0))) { if (isFirst) { /* if first */ print(i); /* print number */ isFirst = false; } else { /* if not first */ print(", " + i); /* print ", " after the previous number */ } } } println("."); } /* lower and upper limits, constants */ private static final int LOW_LIMIT = 1; private static final int HIGH_LIMIT = 100; }