Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekciq;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class AcademyGraduation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Scanner scan = new Scanner(System.in); // Create a Scanner object for input
- int numberOfStudents = Integer.parseInt(scan.nextLine()); // Read the number of students
- // Create a TreeMap to store student names and their grades
- Map<String, List<Double>> graduationList = new TreeMap<>();
- // Iterate over each student
- IntStream.range(0, numberOfStudents)
- .mapToObj(i -> scan.nextLine())
- .forEach(name -> {
- // Read the name of the student
- String studentName = name;
- // Read the grades for the student and convert them to doubles
- List<Double> grades = Arrays.stream(scan.nextLine().split("\\s+"))
- .map(Double::parseDouble)
- .collect(Collectors.toList());
- // Store the student name and their grades in the graduationList
- graduationList.put(studentName, grades);
- });
- // Iterate over the graduationList and print each student's name and their average grade
- graduationList.forEach((name, grades) -> {
- System.out.printf("%s is graduated with %s%n", name, getAverage(grades));
- });
- }
- // Method to calculate the average grade for a list of grades
- private static Double getAverage(List<Double> grades) {
- double sum = 0.0;
- // Calculate the sum of grades
- for (Double grade : grades) {
- sum += grade;
- }
- // Calculate and return the average grade
- return sum / grades.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement