Note No.5 "HashMap value has multiple values using class" [Java]

What you want to do

  1. Enter the number of students and passing score
  2. Enter the number of students for the test score and the number of absenteeism, and pick up and display the students whose test score minus the number of absenteeism * 5 points exceeds the passing score.

code

        Scanner sc = new Scanner(System.in);
        int ppl = sc.nextInt(); //Number of students
        int ps = sc.nextInt(); //Passing score
        
        
        Map<Integer, Stud> map = new LinkedHashMap<>(); //Have a student ID number and Stud class
        for(int i = 1; i <= ppl; i++) {
            int a = sc.nextInt(); //Test score
            int b = sc.nextInt(); //Number of absences
            Stud s = new Stud(a, b); //Instantiation
            map.put(i, s); //Fill the map with student ID numbers and instances
        }
        
        for(int key : map.keySet()) {
            Stud c = map.get(key);
            int d = c.score; //Scores for each student ID number
            int e = c.absent; //Number of absences
            int f = d - e * 5; //Calculate the final score
            if(f <= 0) { //If the score becomes negative as a result of the calculation, set 0 as the lower limit.
                f = 0;
            }
            
            if(f >= ps) {
                System.out.println(key);
            }
        }
    }
}


class Stud { //Student class
        int score; //Score
        int absent; //Number of absences
        
        public Stud(int score, int absent) {
            this.score = score;
            this.absent = absent;
        }
}

Impressions

I first came up with an array method, but it seemed to be complicated, so I tried using Map. I wanted to retrieve them in the order they were packed, so I used LinkedHashMap.

Recommended Posts

Note No.5 "HashMap value has multiple values using class" [Java]
Note to have multiple values in HashMap
[Java] Get multiple values from one return value
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
Return value when using Java No. 1 Optional.ifPresent that was useful in business
[Java] How to use the HashMap class
[Beginner] Java method / class / external library [Note 23]
Sort by multiple conditions using Java Stream
How to use Java Scanner class (Note)