// CSE 142, Autumn 2007, Marty Stepp // // This program reads many integers until a particular "sentinel" // value is reached, then displays the total sum. import java.util.*; // for Scanner public class Sentinel { // when we see this value, we'll stop reading numbers public static final int SENTINEL = -1; public static void main(String[] args) { Scanner console = new Scanner(System.in); int sum = 0; // solution to sentinel problem: read the first number outside the loop // (this is another kind of a fencepost solution) System.out.print("Enter a number (" + SENTINEL + " to quit): "); int number = console.nextInt(); while (number != SENTINEL) { sum = sum + number; System.out.print("Enter a number (" + SENTINEL + " to quit): "); number = console.nextInt(); } System.out.println("The total is " + sum); } }