/* This program uses a "sentinel loop" to prompt for numbers repeatedly until a certain "sentinel" value of -1 is entered. The tricky part is making sure that the sentinel value doesn't get added together with the other numbers. EXPECTED OUTPUT: Enter a number (-1 to quit): 95 Enter a number (-1 to quit): 87 Enter a number (-1 to quit): 43 Enter a number (-1 to quit): 29 Enter a number (-1 to quit): -1 The total was 254 */ import java.util.*; public class SentinelSum { public static final int SENTINEL = -1; public static void main(String[] args) { Scanner console = new Scanner(System.in); int sum = 0; // pulled first input out of loop System.out.print("Enter a number (" + SENTINEL + " to quit): "); int number = console.nextInt(); while (number != SENTINEL) { sum += number; // moved to start of while loop body System.out.print("Enter a number (" + SENTINEL + " to quit): "); number = console.nextInt(); } System.out.println("The total was " + sum); } }