/* 9/23/2019 */ import java.util.Scanner; /** Problem: read in and print out a number of test grades */ public class CountingAndSumming { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // this loop uses a sentinel value (e.g. -1) and a structured read loop System.out.println("Enter a grade, -1 to finish: "); double grade = scanner.nextDouble(); int count=0; // initialize count to ZERO outside of loop double sum=0.0; // initialize sum to ZERO outside of loop while(grade!=-1){ System.out.println("Grade " + grade); sum+=grade; // add the grade to the sum count++; // increment count System.out.println("Enter a grade, -1 to finish: "); grade = scanner.nextDouble(); } // fall out of loop count will hold the number of times loop iterated System.out.println("count = " + count + " Total grades is: " + sum + " Average = " + sum/count); scanner.close(); } }