A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

(1)定义一个学生类Student,属性:姓名(String name)、班级班号(String no)、成绩(double score)
        (2)将若干Student对象存入List集合,并统计每个班级的总分和平均分
                思路:
                        a.采用面向对象的思想
                        b.不推荐使用Map<String,List<Student>> 操作不方便
                        c.推荐使用Map<String,ClassRoom>
                       

2 个回复

倒序浏览
public class Test {
        public static void main(String[] args) {
                Student stu1 = new Student("张三","10001",98);
                Student stu2 = new Student("李四","10002",96);
                Student stu3 = new Student("王五","10003",99);
                List<Student> list = new ArrayList<Student>();
                list.add(stu1);
                list.add(stu2);
                list.add(stu3);
               
                Student stu4 = new Student("赵六","10004",98);
                Student stu5 = new Student("田七","10005",96);
                Student stu6 = new Student("周八","10006",99);
                List<Student> list2 = new ArrayList<Student>();
                list2.add(stu4);
                list2.add(stu5);
                list2.add(stu6);
                ClassRoom cr1 = new ClassRoom(list);
                ClassRoom cr2 = new ClassRoom(list);
               
                Map<String,ClassRoom> map = new HashMap<String,ClassRoom>();
                map.put("班级1", cr1);
                map.put("班级2", cr2);
                Set<Entry<String,ClassRoom>> entrySet = map.entrySet();
               
                for(Entry<String,ClassRoom> entry:entrySet){
                        double totalScore = 0;
                        double avgScore = 0;
                        int stuCount = 0;
                        ArrayList<Student> studentList = (ArrayList<Student>) entry.getValue().getStuList();
                        for (Student student : studentList) {
                                totalScore += student.getScore();
                                stuCount++;
                        }
                        System.out.println(entry.getKey()+"的总分数为: "+totalScore);
                        avgScore = totalScore / stuCount;
                        System.out.println(entry.getKey()+"的平均分数为: "+avgScore);
                }
               
        }
}
class ClassRoom{
        private List<Student> stuList;
        public List<Student> getStuList() {
                return stuList;
        }
        public void setStuList(ArrayList<Student> stuList) {
                this.stuList = stuList;
        }
       
        public ClassRoom(List<Student> stuList) {
                super();
                this.stuList = stuList;
        }
        public ClassRoom() {
                super();
        }
}

class Student{
        private String name;
        private String no;
        private double 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;
        }
        public double getScore() {
                return score;
        }
        public void setScore(double score) {
                this.score = score;
        }
        public Student() {
                super();
        }
        public Student(String name, String no, double score) {
                super();
                this.name = name;
                this.no = no;
                this.score = score;
        }
        @Override
        public String toString() {
                return "Student [name=" + name + ", no=" + no + ", score=" + score + "]";
        }
       
}
回复 使用道具 举报
学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马