...个班级各 4 名学员参赛,计算每个班参赛学员的平均分 谢谢!
发布网友
发布时间:2024-10-23 22:12
我来回答
共1个回答
热心网友
时间:2024-11-02 02:18
package com.baidu.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestMap02 {
public static void main(String[] args) {
List<Student> list=new ArrayList<Student>();
exam(list);
Map<String, ClassRoom> rooms=new HashMap<String, ClassRoom>();
count(rooms, list);
printScore(rooms);
}
/**
* 打印
*/
private static void printScore(Map<String, ClassRoom> rooms) {
Set<Map.Entry<String, ClassRoom>> entrySet=rooms.entrySet();
Iterator<Map.Entry<String, ClassRoom>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, ClassRoom> entry=it.next();
ClassRoom room=entry.getValue();
System.out.println("班号:"+room.getNo()+"总成绩:"+room.getTotal()+"平均成绩:"+room.getTotal()/room.getStus().size());
}
}
/**
* 统计班级的分数
*/
public static void count(Map<String, ClassRoom> rooms,List<Student> list){
for (Student student : list) {
ClassRoom room=rooms.get(student.getNo());
if(null==room){
room=new ClassRoom(student.getNo());
rooms.put(student.getNo(), room);
}
room.setTotal(room.getTotal()+student.getScore());
room.getStus().add(student);
}
}
/**
* 数据源
*/
public static void exam(List<Student> list){
//可以使用循环遍历进去
list.add(new Student(80, "a", "001"));
list.add(new Student(80, "b", "001"));
list.add(new Student(80, "c", "001"));
list.add(new Student(80, "d", "001"));
list.add(new Student(80, "e", "002"));
list.add(new Student(80, "f", "002"));
list.add(new Student(80, "g", "002"));
list.add(new Student(80, "h", "002"));
list.add(new Student(80, "i", "003"));
list.add(new Student(80, "j", "003"));
list.add(new Student(80, "k", "003"));
list.add(new Student(83, "l", "003"));
}
}package com.baidu.map;
public class Student {
private double score;
private String name;
private String no;
public Student() {
}
public Student(double score, String name, String no) {
super();
this.score = score;
this.name = name;
this.no = no;
}
@Override
public String toString() {
return "Student [score=" + score + ", name=" + name + ", no=" + no
+ "]";
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}package com.baidu.map;
import java.util.ArrayList;
import java.util.List;
public class ClassRoom {
private String no;
private double total;
private List<Student> stus;
public ClassRoom() {
stus=new ArrayList<Student>();
}
public ClassRoom(String no) {
this();
this.no = no;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public List<Student> getStus() {
return stus;
}
public void setStus(List<Student> stus) {
this.stus = stus;
}
}