... grade = input.nextInt();     
18
19      // loop until sentinel value read from user   
20      while (grade != -1) { 
21         total = total + grade; // add grade to total 
22         gradeCounter = gradeCounter + 1; // increment counter 
23
24         // prompt for input and read next grade from user
25         System.out.print("Enter grade or -1 to quit: ");
26         grade = input.nextInt(); 
27      } 
28
29      // termination phase 
30      // if user entered at least one grade… 
31      if (gradeCounter != 0) { 
32         // use number with decimal point to calculate average of grades
33         double average = (double) total / gradeCounter;     
34
35         // display total and average (with two digits of precision) 
36         System.out.printf("%nTotal of the %d grades entered is %d%n", 
37            gradeCounter, total); 
38         System.out.printf("Class average ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.