Java编程题:输入行数,依次输入课程 分数,统计每门课程最高分

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.nextLine());

        Map<String,Integer> map = new HashMap<>();
        for(int i=0;i<num;i++) {
            String order = input.nextLine().toLowerCase().trim();

            String[] arr = order.split(" ");

            if (map.containsKey(arr[0])){
                if (map.get(arr[0]) < Integer.parseInt(arr[1])){
                    map.put(arr[0],Integer.parseInt(arr[1]));
                }
            }else{
                map.put(arr[0],Integer.parseInt(arr[1]));
            }
        }
        for(Map.Entry<String,Integer> m : map.entrySet()){
            System.out.println(m.getKey() + " " + m.getValue());
        }
        input.close();
    }
}

 

posted @ 2020-09-16 16:01  liw66  阅读(344)  评论(0编辑  收藏  举报