Java Reference
In-Depth Information
61
// if user entered at least one grade...
62
if
(gradeCounter !=
0
)
63
{
64
// calculate average of all grades entered
65
double
average = (
double
) total / gradeCounter;
66
67
// output summary of results
68
System.out.printf(
"Total of the %d grades entered is %d%n"
,
69
gradeCounter, total);
70
System.out.printf(
"Class average is %.2f%n"
, average);
71
System.out.printf(
"%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n"
,
72
"Number of students who received each grade:"
,
73
"A: "
, aCount,
// display number of A grades
74
"B: ",
bCount,
// display number of B grades
75
"C: "
, cCount,
// display number of C grades
76
"D: "
, dCount,
// display number of D grades
77
"F: "
, fCount);
// display number of F grades
78
}
// end if
79
else
// no grades were entered, so output appropriate message
80
System.out.println(
"No grades were entered"
);
81
}
// end main
82
}
// end class LetterGrades
Enter the integer grades in the range 0-100.
Type the end-of-file indicator to terminate input:
On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter
On Windows type <Ctrl> z then press Enter
99
92
45
57
63
71
76
85
90
100
^Z
Grade Report:
Total of the 10 grades entered is 778
Class average is 77.80
Number of students who received each grade:
A: 4
B: 1
C: 2
D: 1
F: 2
Fig. 5.9
|
LetterGrades
class uses the
switch
statement to count letter grades. (Part 3 of 3.)
Reading Grades from the User
Lines 19-23 prompt the user to enter integer grades and to type the end-of-file indicator
to terminate the input. The
end-of-file indicator
is a system-dependent keystroke combi-