/*
其中,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));
}
}
其中,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));
}
}