本帖最后由 彼岸轮回 于 2016-6-19 23:45 编辑
ArrayList<Student> al = new ArrayList<Student>(); al.add(new Student("张三","class1",68,23));
al.add(new Student("小明","class2",88,24));
al.add(new Student("李四","class1",99,25));
al.add(new Student("小红","class2",85,18));
al.add(new Student("王五","class1",66,20));
al.add(new Student("狗蛋","class2",79,26));
al.add(new Student("赵六","class1",88,26));
al.add(new Student("日天","class3",60,1));
al.add(new Student("日地","class3",90,1));
al.add(new Student("日空气","class3",66,1));
int sumage = 0;
Map<String, Integer> m1 = new HashMap<>();
Map<String, Integer> m2 = new HashMap<>();
for(Student s : al){
int age = s.getAge();
m1.put(s.getNo(),m1.containsKey(s.getNo())?m1.get(s.getNo())+1 : 1);
m2.put(s.getNo(),m2.containsKey(s.getNo())?m2.get(s.getNo())+s.getScore() : s.getScore());
sumage += age;
}
System.out.println("所有学生的平均年龄为:"+sumage/al.size());
for(String s : m1.keySet()){
System.out.println(s + "....总分为:" + m2.get(s) + "....平均分为:" + m2.get(s)/m1.get(s));
}
|