-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSD1 Java Programming Task
More file actions
74 lines (60 loc) · 2.42 KB
/
Copy pathSD1 Java Programming Task
File metadata and controls
74 lines (60 loc) · 2.42 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
64
65
66
67
68
69
70
71
72
73
74
import java.util.Scanner;
public class StudentMarks {
public static String[] studentNames = new String[10];
public static double[] studentMarks = new double[10];
public static int studentCount = 0;
public static double calculateAverage(String studentName) {
for (int i = 0; i < studentCount; i++) {
if (studentNames[i].equalsIgnoreCase(studentName)) {
return studentMarks[i];
}
}
return -1;
}
public static double calculateClassAverage() {
double totalMarks = 0;
for (int i = 0; i < studentCount; i++) {
totalMarks += studentMarks[i];
}
return studentCount > 0 ? totalMarks / studentCount : 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.print("Enter student name: ");
studentNames[i] = scanner.nextLine();
System.out.print("Enter marks for " + studentNames[i] + ": ");
studentMarks[i] = scanner.nextDouble();
scanner.nextLine();
studentCount++;
}
boolean addMoreStudents = true;
while (addMoreStudents && studentCount < studentNames.length) {
System.out.print("Enter additional student name (or type 'exit' to stop): ");
String additionalName = scanner.nextLine();
if (additionalName.equalsIgnoreCase("exit")) {
addMoreStudents = false;
}
else {
System.out.print("Enter marks for " + additionalName + ": ");
double additionalMarks = scanner.nextDouble();
scanner.nextLine();
studentNames[studentCount] = additionalName;
studentMarks[studentCount] = additionalMarks;
studentCount++;
}
}
System.out.print("Enter a student name to check their marks: ");
String studentName = scanner.nextLine();
double studentAverage = calculateAverage(studentName);
if (studentAverage != -1) {
System.out.println(studentName + "'s mark is: " + studentAverage);
}
else {
System.out.println(studentName+ " not found.");
}
double classAverage = calculateClassAverage();
System.out.println("Class average mark: " + classAverage);
scanner.close();
}
}