|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Locale; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + record Student(String name, int score) { |
| 8 | + } |
| 9 | + |
| 10 | + static int readPositiveCount(Scanner scanner) { |
| 11 | + while (true) { |
| 12 | + System.out.print("How many students? "); |
| 13 | + String raw = scanner.nextLine().trim(); |
| 14 | + try { |
| 15 | + int value = Integer.parseInt(raw); |
| 16 | + if (value > 0) { |
| 17 | + return value; |
| 18 | + } |
| 19 | + System.out.println("Please enter a number greater than zero."); |
| 20 | + } catch (NumberFormatException error) { |
| 21 | + System.out.println("Please enter a valid integer."); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + static String readNonEmpty(Scanner scanner, String prompt) { |
| 27 | + while (true) { |
| 28 | + System.out.print(prompt); |
| 29 | + String value = scanner.nextLine().trim(); |
| 30 | + if (!value.isEmpty()) { |
| 31 | + return value; |
| 32 | + } |
| 33 | + System.out.println("This value cannot be empty."); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static int readScore(Scanner scanner, String prompt) { |
| 38 | + while (true) { |
| 39 | + System.out.print(prompt); |
| 40 | + String raw = scanner.nextLine().trim(); |
| 41 | + try { |
| 42 | + int score = Integer.parseInt(raw); |
| 43 | + if (score >= 0 && score <= 100) { |
| 44 | + return score; |
| 45 | + } |
| 46 | + System.out.println("Score must be between 0 and 100."); |
| 47 | + } catch (NumberFormatException error) { |
| 48 | + System.out.println("Please enter a valid integer score."); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + Locale.setDefault(Locale.US); |
| 55 | + Scanner scanner = new Scanner(System.in); |
| 56 | + List<Student> students = new ArrayList<>(); |
| 57 | + int count = readPositiveCount(scanner); |
| 58 | + |
| 59 | + for (int index = 1; index <= count; index++) { |
| 60 | + String name = readNonEmpty(scanner, "Student #" + index + " name: "); |
| 61 | + int score = readScore(scanner, name + "'s score (0-100): "); |
| 62 | + students.add(new Student(name, score)); |
| 63 | + } |
| 64 | + |
| 65 | + int total = 0; |
| 66 | + Student highest = students.get(0); |
| 67 | + Student lowest = students.get(0); |
| 68 | + int passed = 0; |
| 69 | + |
| 70 | + System.out.println("Students:"); |
| 71 | + for (Student student : students) { |
| 72 | + System.out.println("- " + student.name() + ": " + student.score()); |
| 73 | + total += student.score(); |
| 74 | + if (student.score() > highest.score()) { |
| 75 | + highest = student; |
| 76 | + } |
| 77 | + if (student.score() < lowest.score()) { |
| 78 | + lowest = student; |
| 79 | + } |
| 80 | + if (student.score() >= 60) { |
| 81 | + passed++; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + System.out.printf("Average: %.2f%n", (double) total / students.size()); |
| 86 | + System.out.println("Highest: " + highest.name() + " (" + highest.score() + ")"); |
| 87 | + System.out.println("Lowest: " + lowest.name() + " (" + lowest.score() + ")"); |
| 88 | + System.out.println("Passed: " + passed + "/" + students.size()); |
| 89 | + } |
| 90 | +} |
0 commit comments