/* CSE 142, Autumn 2007, Marty Stepp This version of our sentinel program shows a loop with a break statement. You should not use the break statement on your homework assignments. */ 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; // an infinite loop that breaks immediately if the sentinel is read; // somewhat of a hack, but it works while (true) { System.out.print("Enter a number (" + SENTINEL + " to quit): "); int number = console.nextInt(); if (number == SENTINEL) { break; // stop this loop, I want out! } sum = sum + number; } System.out.println("The total is " + sum); } }