* 1.在这个list 的基础上,完成下列要求:
* 1) 计算所有学生的平均年龄
* 2) 计算各个班级的平均分
* 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"));
*/
public class PracticeDemo01 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("Tom", 18, 100.0, "class05"));
list.add(new Student("Jerry", 22, 70.0, "class04"));
list.add(new Student("Owen", 25, 90.0, "class05"));
list.add(new Student("Jim", 30,80.0, "class05"));
list.add(new Student("Steve", 28, 66.0, "class06"));
list.add(new Student("Kevin", 24, 100.0, "class04"));
//年龄
int sumAge = 0;
//各个班级的总成绩
double sum05 = 0.0;
double sum04 = 0.0;
double sum06 = 0.0;
//统计各个班级的人数
int count05 = 0;
int count04 = 0;
int count06 = 0;
//遍历list集合得到每一个学生
for(Student student : list){
//获取学生年龄
Integer age = student.getAge();
sumAge += age;
//获取每个班级
String classNum = student.getClassNum();
if (classNum.equals("class05")){ //5班
//获取成绩
Double score = student.getScore();
sum05 += score;
count05++;
}else if(classNum.equals("class04")){//4班
Double score = student.getScore();
sum04 += score;
count04++;
}else if(classNum.equals("class06")){//6班
Double score = student.getScore();
sum06 += score;
count06++;
}
}
//1) 计算所有学生的平均年龄
System.out.println("所有学生的平均年龄:" + sumAge / list.size());
// 2) 计算各个班级的平均分
System.out.println("5班的平均分:" + sum05 / count05);
System.out.println("4班的平均分:" + sum04 / count04);
System.out.println("6班的平均分:" + sum06 / count06);
}
}
* 1.在这个list 的基础上,完成下列要求: * 1) 计算所有学生的平均年龄 * 2) 计算各个班级的平均分
最新推荐文章于 2022-08-08 19:40:29 发布