Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DIDA-lJ opened this issue Nov 14, 2023 · 0 comments
Open

Comments

@DIDA-lJ
Copy link
Contributor

DIDA-lJ commented Nov 14, 2023

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);
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant