classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();

package a927;

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

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

    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 class A {

    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" )) ;
        
        double  sum = 0 ;
        
        for(int i = 0 ; i<list.size();i++)
        {
            sum += list.get(i).getAge();
        }

        System.out.println("平均年龄为:" + sum/list.size());
        int sum4 = 0 ;
        int j4 = 0 ;
        int sum5 = 0 ;
        int j5 = 0 ;
        int sum6 = 0 ;
        int j6 = 0 ;
        for(Student x  : list)
        {
            if(x.getClassNum().equals("class05") )
            {
                sum5 +=x.getScore( ) ;    
                ++ j5  ;
            }
            if(x.getClassNum().equals("class04") )
            {
                sum4 +=x.getScore( ) ;    
                ++ j4  ;
            }
            if(x.getClassNum().equals("class06") )
            {
                sum6 +=x.getScore( ) ;    
                ++ j6  ;
            }
        }
        System.out.println("class05的平均成绩:"+sum5/j5);
        System.out.println("class04的平均成绩:"+sum4/j4);
        System.out.println("class06的平均成绩:"+sum6/j6);
    }

}

 

posted @ 2016-09-27 16:25  丶疏影横斜  阅读(1824)  评论(0编辑  收藏  举报