展开全部
Student.java类62616964757a686964616fe58685e5aeb931333363386666:public class Student {
private String name;
private String className;
private String courseName;
private int score;
public Student(){
}
public Student(String name, String className, String courseName, int score) {
this.name = name;
this.className = className;
this.courseName = courseName;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
main类:import java.util.ArrayList;
public class StudentMain {
public static void main(String[] args){
//初始化学生信息
Student student1 = new Student("张三", "class1", "Java", 85);
Student student2 = new Student("周乐儿", "class1", "C#", 79);
Student student3 = new Student("王涛", "class2", "C#", 52);
Student student4 = new Student("李明", "class2", "Java", 48);
ArrayList students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
//(1) 请列出所有学生的信息;
System.out.println("(1) 请列出所有学生的信息;");
System.out.println("姓名 班级名 课程名 考试成绩");
for (Student student : students) {
System.out.println(student.getName() + " " + student.getClassName() + " " + student.getCourseName() + " " +
student.getScore());
}
//(2) 请打印class1班的总分和平均分;
System.out.println("(2)请打印class1班的总分和平均分;");
ArrayList class1StudentScore = new ArrayList<>();
int class1StudentTotalScore = 0;
for (Student student : students) {
if (student.getClassName().equals("class1")){
class1StudentTotalScore += student.getScore();
class1StudentScore.add(student.getScore());
}
}
System.out.println("总分:" + class1StudentTotalScore);
System.out.println("class1平均分:" + averageScore(class1StudentScore));
//(3) 请打印Java、C#课程的平均分。
System.out.println("(3) 请打印Java、C#课程的平均分。");
ArrayList javaStudentTotalScore = new ArrayList<>();
ArrayList cSharpStudentTotalScore = new ArrayList<>();
for (Student student : students) {
if (student.getCourseName().equals("Java")){
javaStudentTotalScore.add(student.getScore());
}
if (student.getCourseName().equals("C#")){
cSharpStudentTotalScore.add(student.getScore());
}
}
System.out.println("java平均分:" + averageScore(javaStudentTotalScore));
System.out.println("c#平均分:" + averageScore(cSharpStudentTotalScore));
}
//计算平均分
private static double averageScore(ArrayList list){
double average = 0.0;
for (int i = 0; i
average += list.get(i) ;
}
//考虑size为0的情况
if (list.size() == 0){
return 0.0;
}
return average/list.size();
}
}
运行结果: