kamacoder-solutions icon indicating copy to clipboard operation
kamacoder-solutions copied to clipboard

7.平均绩点,用 HashMap 的方法写出来,可读性更高一点

Open DIDA-lJ opened this issue 1 year ago • 0 comments

7.平均绩点


import java.util.*;
import java.lang.*;


public class Main{
    public static Map<String,Integer> map = new HashMap<>();
    static{
        map.put("A",4);
        map.put("B",3);
        map.put("C",2);
        map.put("D",1);
        map.put("F",0);
    }
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] items = line.split(" ");
            double sum = 0.0;
            int count = 0;
            boolean flag = false;
            for(String item:items){
                if(map.get(item) != null){
                    sum += map.get(item);
                    count++;
                }else{
                    flag = true;
                }
            }
            if (flag) {
                System.out.println("Unknown");
            } else {
                System.out.printf("%.2f\n", sum / count);
            }
        }
    }
}

DIDA-lJ avatar Nov 14 '23 10:11 DIDA-lJ