-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradetracker.java
More file actions
54 lines (45 loc) · 1.65 KB
/
Gradetracker.java
File metadata and controls
54 lines (45 loc) · 1.65 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
//develop a program that allows a teacher to enter students grades and compute their average ,highest,and lowest scores.you can use ArrayList
//or arraylists to store data.
import java.util.ArrayList;
import java.util.Scanner;
public class Gradetracker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Double> grades = new ArrayList<>();
String input;
System.out.println("Enter student grades (type 'done' to finish):");
while (true) {
input = scanner.nextLine();
if (input.equalsIgnoreCase("done")) {
break;
}
try {
double grade = Double.parseDouble(input);
grades.add(grade);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number or 'done' to finish.");
}
}
if (grades.isEmpty()) {
System.out.println("No grades entered.");
return;
}
double sum = 0;
double highest = grades.get(0);
double lowest = grades.get(0);
for (double grade : grades) {
sum += grade;
if (grade > highest) {
highest = grade;
}
if (grade < lowest) {
lowest = grade;
}
}
double average = sum / grades.size();
System.out.printf("Average: %.2f%n", average);
System.out.printf("Highest: %.2f%n", highest);
System.out.printf("Lowest: %.2f%n", lowest);
scanner.close();
}
}