Didart

Average Students Grades

Jan 17th, 2023
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package SetsAndMaps3;
  2.  
  3. import java.util.*;
  4.  
  5. public class AverageStudentsGrades {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, List<Double>> students = new TreeMap<>();
  10.  
  11.         int num = Integer.parseInt(scanner.nextLine());
  12.  
  13.         for (int index = 0; index < num; index++) {
  14.             String[] input = scanner.nextLine().split("\\s+");
  15.  
  16.             String name = input[0];
  17.             double grade = Double.parseDouble(input[1]);
  18.             students.putIfAbsent(name, new ArrayList<>());
  19.             students.get(name).add(grade);
  20.         }
  21.  
  22.         for (Map.Entry<String, List<Double>> entry : students.entrySet()) {
  23.  
  24.             System.out.print(entry.getKey() + " -> ");
  25.  
  26.             double average = 0;
  27.             for (Double s : entry.getValue()) {
  28.                 System.out.printf("%.2f ", s);
  29.                 average += s;
  30.             }
  31.             System.out.printf("(avg: %.2f)%n", average / entry.getValue().size());
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment