编写一个java程序计算出6名学生两门课的平均成绩以及每门课的最高成绩
时间: 2024-09-23 11:02:02 浏览: 37
为了编写一个Java程序,我们首先需要定义一个`Student`类,包含姓名、两门课程的成绩等属性。然后,我们可以创建一个`Course`类来存储课程名称和学生的成绩。接下来,我们将编写一个`Main`类来计算六名学生两门课的平均成绩以及每门课的最高分。
```java
public class Student {
String name;
Course course1;
Course course2;
// 构造函数
public Student(String name, int course1Score, int course2Score) {
this.name = name;
this.course1 = new Course("课程1", course1Score);
this.course2 = new Course("课程2", course2Score);
}
// 省略getter和setter方法...
}
class Course {
private String name;
private int score;
// 构造函数和getter/setter方法
public Course(String name, int score) {
this.name = name;
this.score = score;
}
public double getAverageScore(Student[] students) {
int totalScore = 0;
for (Student student : students) {
totalScore += this.score;
}
return (double) totalScore / students.length;
}
public int getHighestScore(Student[] students) {
int maxScore = Integer.MIN_VALUE;
for (Student student : students) {
if (student.getCourse(this.name).getScore() > maxScore) {
maxScore = student.getCourse(this.name).getScore();
}
}
return maxScore;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[6];
// 填充学生数据...
for (int i = 0; i < students.length; i++) {
students[i] = new Student("学生" + (i+1), 85, 90); // 示例分数
}
Course course1 = students[0].course1; // 假设所有学生都上相同的课程1
Course course2 = students[0].course2; // 假设所有学生都上相同的课程2
double averageScore1 = course1.getAverageScore(students);
double averageScore2 = course2.getAverageScore(students);
int highestScore1 = course1.getHighestScore(students);
int highestScore2 = course2.getHighestScore(students);
System.out.println("课程1平均成绩: " + averageScore1);
System.out.println("课程1最高分: " + highestScore1);
System.out.println("课程2平均成绩: " + averageScore2);
System.out.println("课程2最高分: " + highestScore2);
}
}
```
在这个例子中,我们假设每个学生都有相同的课程1和课程2的成绩。实际应用中,你需要根据实际情况填充学生数据,并可能针对每位学生有不同的课程安排。
阅读全文