/**
 * GradeAveragerFencepost.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 of the program uses a fencepost loop -- putting the reading
 * of the first grade before the while loop so that the while condition
 * can check for the sentinel.
 *
 * Computer Science S-111
 */

import java.util.*;

public class GradeAveragerFencepost {
    public static void main(String[] args) {
	Scanner console = new Scanner(System.in);
	int total = 0;
	int numGrades = 0;

	System.out.print("Enter a grade (or -1 to quit): ");
	int grade = console.nextInt();
	while (grade != -1) {
	    total += grade;
	    numGrades++;
	    System.out.print("Enter a grade (or -1 to quit): ");
	    grade = console.nextInt();
	}

	if (numGrades > 0) {
	    System.out.println("\nThe average is ");
	    System.out.println((double)total / numGrades);
	}
    }
}
