-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassAverage2.java
More file actions
30 lines (25 loc) · 901 Bytes
/
ClassAverage2.java
File metadata and controls
30 lines (25 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Chapter4;
import java.util.Scanner;
public class ClassAverage2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total = 0;
int gradeCounter = 0;
System.out.print("Enter grade (or -1 to quit): ");
int grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCounter = 1 + gradeCounter;
System.out.print("Enter grade (or -1 to quit): ");
grade = input.nextInt();
}
if (gradeCounter != 0) {
double average = (double) total/gradeCounter;
System.out.printf("%nTotal of the %d grades entered is %d%n", gradeCounter, total);
System.out.printf("Class average is %.3f%n", average);
}
else {
System.out.println("No grades were entered");
}
}
}