-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeCalculator.java
More file actions
63 lines (52 loc) · 1.75 KB
/
GradeCalculator.java
File metadata and controls
63 lines (52 loc) · 1.75 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("How many students are in the class?");
int numberOfStudents = scanner.nextInt();
double[] grades = new double[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter grade for student #" + (i + 1) + ":");
grades[i] = scanner.nextDouble();
}
scanner.close();
double max = getMaxScore(grades);
double min = getMinScore(grades);
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Student #" + (i + 1) + ": " + getLetterGrade(grades[i], max));
}
System.out.println("Highest score: " + max);
System.out.println("Lowest score: " + min);
}
public static double getMaxScore(double[] grades) {
double max = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] > max) {
max = grades[i];
}
}
return max;
}
public static double getMinScore(double[] grades) {
double min = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] < min) {
min = grades[i];
}
}
return min;
}
public static String getLetterGrade(double grade, double max) {
if (grade >= max - 5) {
return "A";
} else if (grade >= max - 10) {
return "B";
} else if (grade >= max - 15) {
return "C";
} else if (grade >= max - 20) {
return "D";
} else {
return "F";
}
}
}