... // 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 is %.2f%n", average);
39         }
40         else { // no grades were entered, so output appropriate message
41            System.out.println("No grades were entered");
42         }
43      }
44   }

Enter grade or -1 to quit: 97
Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1

Total of the 3 grades entered is 257
Class average is 85.67

Program Logic for Sentinel-Controlled Iteration vs. Counter-Controlled Iteration

Line 12 initializes gradeCounter ...

Get Java How To Program, Late 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.