第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,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) 计算各个班级的平均分 

package com.hanqi.test;

public class Student {
     private String name;  
        private int age;  
        private int score;  
    private String classNum;
    
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getClassNum() {
        return classNum;
    }
    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }
    public Student() {
        super();
        
    }
    public Student(String name, int age, int score, String classNum) {
        super();
        this.name = name;
        this.age = age;
        this.score = score;
        this.classNum = classNum;
    }
    



}
package com.hanqi.test;

import java.util.ArrayList;
import java.util.List;

public class TestStudent {

    public static void main(String[] args) {
        
        List<Student>  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"));
        
        
        int sum=0;
        for(int i=0;i<list.size();i++)
        {
            sum+=list.get(i).getAge();
        }
        System.out.println("所有学生的平均分是:"+sum/list.size());
        
        int score4=0;
        int score5=0;
        int score6=0;
        int num4=0;
        int num5=0;
        int num6=0;
        for(int j=0;j<list.size();j++)
        {
        Student st=list.get(j);
        
        if(st.getClassNum()=="class04")
        {
            num4++;
            score4 +=st.getScore();
        }
        else if(st.getClassNum()=="class05")
        {
            num5++;
            score5+=st.getScore();
        }
        else
        {
            num6++;
            score6+=st.getScore();
        }
       
        }
        System.out.println("class04的平均分数是:"+score4/num4);
        System.out.println("class05的平均分数是:"+score5/num5);
        System.out.println("class06的平均分数是:"+score6/num6);
        

    }

}

 

posted @ 2016-10-05 22:47  -加勒比海带  阅读(5003)  评论(0编辑  收藏  举报