20 求平均年龄 各班平均分

/*


其中,classNum 表示学生的班号,例如"class05"。
有如下List
List list = new ArrayList();
list.add(new Student("Tom", 18, 100, "class05"));
list.add(new Student("Jerry", 22, 70, "class04"));
list.add(new Student("Owen", 25, 90, "class05"));
list.add(new Student("Jim", 30,80 , "class05"));
list.add(new Student("Steve", 28, 66, "class06"));
list.add(new Student("Kevin", 24, 100, "class04"));
在这个list 的基础上,完成下列要求:
1) 计算所有学生的平均年龄
2) 计算各个班级的平均分






 */

import java.util.*;

class Student {
    private String name, classNum;
    private int age;
    private double score;

    Student(String name, int age, double score, String classNum) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.classNum = classNum;
    }

    public int getAge() {
        return age;

    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getClassNum() {
        return classNum;

    }

    public void setClassNum(String classNum) {
        this.classNum = classNum;

    }

    public double getScore() {

        return score;
    }

}

public class A {
    // 简化打印代码
    static void sop(Object o) {
        System.out.println(o);
    }

    // 求平均年龄
    static int getAverageAge(List<Student> list) {
        int sum = 0, count = 0;
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            count++;

            sum = sum + it.next().getAge();

        }
        return sum / count;

    }

    // 求各班平均分
    static void getPerClassNumScore(List<Student> list) {
        Set<String> set = new TreeSet<String>();
        Student stu;
        Iterator<Student> it = list.iterator();

        while (it.hasNext()) {
            stu = it.next();
            set.add(stu.getClassNum());
        }
        // Iterator <String> setit= set.iterator();
        for (String classNum : set) {
            sop(classNum + "'s per score" + PerScore(list, classNum));// 调用方法

        }

    }

    // 求输入班级后的平均分
    static double PerScore(List<Student> list, String classNum) {

        double sum = 0, count = 0;
        Student stu;

        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            stu = it.next();
            if (stu.getClassNum().equals(classNum)) {
                count++;
                sum = sum + stu.getScore();
            }

        }
        return sum / count;

    }

    public static void main(String args[]) {

        List<Student> list = new ArrayList<Student>();
        list.add(new Student("Tom", 18, 100, "class05"));
        list.add(new Student("Jerry", 22, 70, "class04"));
        list.add(new Student("Owen", 25, 90, "class05"));
        list.add(new Student("Jim", 30, 80, "class05"));
        list.add(new Student("Steve", 28, 66, "class06"));
        list.add(new Student("Kevin", 24, 100, "class04"));

        getPerClassNumScore(list);
        sop("平均年龄" + getAverageAge(list));

    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值