1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package test;
import java.util.HashMap;
public class Student_Grade { private HashMap<String, HashMap<String, Double>> studentInfo = new HashMap<String, HashMap<String, Double>>();
public void add(String name, String course, double grade) { if (studentInfo.containsKey(name)) { studentInfo.get(name).put(course, grade); } else { HashMap<String, Double> temp = new HashMap<String, Double>(); temp.put(course, grade); studentInfo.put(name, temp); } }
public String getScore_name(String name) { HashMap<String, Double> temp = new HashMap<String, Double>(); for (String k : studentInfo.keySet()) { if (k == name) { temp = studentInfo.get(name); break; } } return name+" "+temp.toString(); }
public String getScore_subject(String subject) { HashMap<String, Double> temp = new HashMap<String, Double>(); for (String k : studentInfo.keySet()) { if (studentInfo.get(k).containsKey(subject)) { temp.put(k, studentInfo.get(k).get(subject)); } } return subject+":"+temp.toString(); }
public String toString() { String temp=""; for(String k:studentInfo.keySet()) temp+=(k+":"+studentInfo.get(k).toString()+"\n"); return temp; }
public static void main(String[] args) { Student_Grade student = new Student_Grade(); student.add("张三", "Java", 60.0); student.add("张三", "C++", 70.0); student.add("李四", "C++", 80.0); student.add("王五", "C++", 90.2); System.out.println(student.getScore_name("张三")); System.out.println(student.getScore_subject("C++")); System.out.println(student); } }
|