Java Reference
In-Depth Information
9
int total = 0 ; // sum of grades
int gradeCounter = 0 ; // number of grades entered
int aCount = 0 ; // count of A grades
int bCount = 0 ; // count of B grades
int cCount = 0 ; // count of C grades
int dCount = 0 ; // count of D grades
int fCount = 0 ; // count of F grades
10
11
12
13
14
15
16
17
Scanner input = new Scanner(System.in);
18
19
System.out.printf( "%s%n%s%n %s%n %s%n" ,
20
"Enter the integer grades in the range 0-100." ,
21
"Type the end-of-file indicator to terminate input:" ,
22
"On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter" ,
23
"On Windows type <Ctrl> z then press Enter" );
24
25
// loop until user enters the end-of-file indicator
26
while (
input.hasNext()
)
27
{
28
int grade = input.nextInt(); // read grade
29
total += grade; // add grade to total
30
++gradeCounter; // increment number of grades
31
32
// increment appropriate letter-grade counter
switch (grade / 10 )
{
case 9 : // grade was between 90
case 10 : // and 100, inclusive
++aCount;
break ; // exits switch
case 8 : // grade was between 80 and 89
++bCount;
break ; // exits switch
case 7 : // grade was between 70 and 79
++cCount;
break ; // exits switch
case 6 : // grade was between 60 and 69
++dCount;
break ; // exits switch
default : // grade was less than 60
++fCount;
break ; // optional; exits switch anyway
} // end switch
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
} // end while
57
58
// display grade report
59
System.out.printf( "%nGrade Report:%n" );
60
Fig. 5.9 | LetterGrades class uses the switch statement to count letter grades. (Part 2 of 3.)
Search WWH ::




Custom Search