package homework003; import java.util.ArrayList;
import java.util.List; public class Text { 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 num1=0;
int num2=0;
int num3=0;
int score1=0;
int score2=0;
int score3=0;
for(int i =0;i<list.size();i++)
{
Student st =list.get(i);
if(st.getClassNum()=="class04")
{
num1++;
score1+=st.getScore();
}
else if(st.getClassNum()=="class05")
{
num2++;
score2+=st.getScore();
}
else
{
num3++;
score3+=st.getScore();
}
}
System.out.println("class04的平均分:"+(score1/num1));
System.out.println("class05的平均分:"+(score2/num2));
System.out.println("class06的平均分:"+(score3/num3)); } }
package homework003; 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(String name, int age, int score, String classNum) {
super();
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
public Student() {
super();
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + ", classNum=" + classNum + "]";
} }