/**
 * GradeAveragerFlag.java
 *
 * Uses a while loop to read in a series of grades from the user, 
 * and prints out their average.  It continues reading values until the user
 * enters the sentinel value of -1.
 *
 * This version uses a boolean flag to control the while loop.
 *
 * Computer Science S-111
 */

import java.util.*;

public class GradeAveragerFlag {
    public static void main(String[] args) {
	Scanner console = new Scanner(System.in);
	int total = 0;
	int numGrades = 0;

	// We use this boolean flag to keep track of whether the user is done.
	boolean done = false;

	while (!done) {
	    System.out.print("Enter a grade (or -1 to quit): ");
	    int grade = console.nextInt();
	    if (grade == -1) {         // sentinel value, so break out of loop
		done = true;
	    } else {
		total += grade;
		numGrades++;
	    }
	}

	if (numGrades > 0) {
	    System.out.println("\nThe average is ");
	    System.out.println((double)total / numGrades);
	}
    }
}
