【154天】尚学堂高琪Java300集视频精华笔记(121)【运用思维模型01:以终为始,逐级分解】...

一点心得

  1. 我发现我很享受这个不看视频,自我探索式的还原老师的代码的过程。

  2. 这一次我对“以终为始,逐级分解”这个思维模型进行了刻意练习。

第121集:HashMap、经典存储、经典分拣思路、与面向对象组合解题

思维模型:以终为始,逐级分解

条件

一堆学生,有不同的班级,分数,不同的班级有班号

结果

统计出每个班级的总分和平均分

分解
  1. 问:为获得预期结果,目前可知道有哪些内容需要代码表示?

    答:班级、学生、个人分数、班级总分、平均分、班号、班级的学生人数、

  2. 问:这些内容如何形成用Java的对象装起来。
    答:

    1. 对象

      1. 班级

        1. 属性

          1. 装学生的容器(这里可得学生人数,进而算出平均分)

          2. 班号

          3. 总分

        2. 方法

          1. 将学生添加进对应班级的方法——>根据学生的班号,将班级与学生对应起来——>根据学生的班号,创建对应班级,再将班级与学生对应起来——>根据学生的班号,创建对应班级,再将学生添加进新建出来的班级中——>

          2. 计算平均分的方法

          3. 打印平均分的方法

          4. set/get方法

          5. 带参与不带参构造函数

      2. 学生

        1. 属性

          1. 所属班级班号

          2. 个人分数

        2. 方法

          1. set/get方法

          2. 带参与不带参构造函数

  3. 问:接下来呢?
    答:想不明白时写代码,寻求进行下一步的思路


package test121;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClassRoom {
    private String no;
    private int totalScore;
    private List<Student> list;
    
    public ClassRoom(){
        list = new ArrayList<Student>();
    }
    
    public ClassRoom(String no){
        this.no = no;
    }

    public void setNo(String no){
        this.no = no;
    }
    
    public String getNo(){
        return no;
    }


    public int getTotalScore() {
        return totalScore;
    }


    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }


    public void setList(List<Student> list){
        this.list = list;
    }
    
    public List<Student> getList(){
        return list;
    }

    
    //******
    
    public static void main(String[] args){
        List<Student> testList = new ArrayList<Student>();
        exam(testList);
        
        //将学生添加进对应班级的方法
        //根据学生的班号,创建对应班级,再将学生添加进新建出来的班级中
        Map<String,ClassRoom> rooms = new HashMap<String,ClassRoom>();
        
        //把班号与班级形成键值对,通过键能找到班级,也能通过键找到学生
        
        statistics(testList,rooms);
        
        //至此已将总分算出来了,数据存在room的totalScore属性里
        //将每个班级的这个属性取出来打印。
        //遍历rooms
        
        printScore(rooms);//打印Map容器中的班级分数情况
        
        
    }
    
    //创建学生的数据
    public static void exam(List<Student> testList){
        testList.add(new Student("273",98));
        testList.add(new Student("283",88));
        testList.add(new Student("243",28));
        testList.add(new Student("273",58));
        testList.add(new Student("283",68));
        testList.add(new Student("243",98));
    }
    
    
    public static void statistics(List<Student> testList,Map<String,ClassRoom> rooms){
        for(Student temp:testList){
            String classNo = temp.getNo();
            if(rooms.get(classNo)==null){//判断根据学生的班号能否找到一个班级,若有,直接累加分数,若无,新建一个班级,再累加分数
                ClassRoom room = new ClassRoom();
                rooms.put(classNo, room);
                room.setTotalScore(room.getTotalScore()+temp.getScore());
                room.getList().add(temp);
            } else {
                ClassRoom room = rooms.get(classNo);
                room.setTotalScore(room.getTotalScore()+temp.getScore());
                room.getList().add(temp);
            }
        }
    }
    
    public static void printScore(Map<String,ClassRoom> rooms){
        Set<Map.Entry<String,ClassRoom>> a =rooms.entrySet();
        Iterator<Map.Entry<String,ClassRoom>> itr = a.iterator();
        while(itr.hasNext()){
            Map.Entry<String, ClassRoom> tempRoom = itr.next();
            String tempNo = tempRoom.getKey();
            int tempTotalScore = tempRoom.getValue().getTotalScore();
            int tempNum = tempRoom.getValue().getList().size();
            System.out.println("班级:"+tempNo+" 总分:"+tempTotalScore+" 平均分:"+tempTotalScore/tempNum);
        }
    }

}
package test121;

public class Student {
    private String no;
    private int score;
    
    public void setNo(String no){
        this.no = no;
    }
    
    public String getNo(){
        return no;
    }
    
    public void setScore(int score){
        this.score = score;
    }
    
    public int getScore(){
        return score;
    }
    
    public Student(){
        
    }
    
    public Student(String no,int score){
        this.no = no;
        this.score = score;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值