-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradeCalculator.java
51 lines (44 loc) · 1.48 KB
/
GradeCalculator.java
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
import java.util.*;
class Student {
String name;
float grade;
Student(String name, float grade) {
this.name = name;
this.grade = grade;
}
}
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
System.out.print("Enter the total number of students : ");
int cnt = scanner.nextInt();
for(int i = 1;i<=cnt;i++){
System.out.print("Enter student's name: ");
String name = scanner.next();
System.out.print("Enter " + name + "'s grade: ");
float grade = scanner.nextFloat();
students.add(new Student(name, grade));
}
if (students.isEmpty()) {
System.out.println("No student data was entered.");
return;
}
float highest = students.get(0).grade;
float lowest = students.get(0).grade;
double sum = 0;
for (Student student : students) {
if (student.grade > highest) {
highest = student.grade;
}
if (student.grade < lowest) {
lowest = student.grade;
}
sum += student.grade;
}
double average = sum / students.size();
System.out.println("Average score: " + average);
System.out.println("Highest score: " + highest);
System.out.println("Lowest score: " + lowest);
}
}