定义数组存储4个学生的成绩 学生的属性分别有:姓名,语文,数学,英语,java 要求1:把他们所有信息遍历出来 要求2:计算出他们个人的平均成绩 要求3:比较谁的平均成绩最高
时间: 2024-05-16 09:16:20 浏览: 77
以下是使用Java语言实现的代码:
```java
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
private int javaScore;
public Student(String name, int chineseScore, int mathScore, int englishScore, int javaScore) {
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.javaScore = javaScore;
}
public String getName() {
return name;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getJavaScore() {
return javaScore;
}
public double getAverageScore() {
return (chineseScore + mathScore + englishScore + javaScore) / 4.0;
}
public static void main(String[] args) {
Student[] students = {
new Student("张三", 80, 90, 85, 95),
new Student("李四", 75, 85, 90, 80),
new Student("王五", 90, 95, 80, 85),
new Student("赵六", 95, 80, 85, 90)
};
// 遍历所有学生的信息
for (Student student : students) {
System.out.println("姓名:" + student.getName());
System.out.println("语文成绩:" + student.getChineseScore());
System.out.println("数学成绩:" + student.getMathScore());
System.out.println("英语成绩:" + student.getEnglishScore());
System.out.println("Java成绩:" + student.getJavaScore());
System.out.println("平均成绩:" + student.getAverageScore());
System.out.println();
}
// 比较平均成绩最高的学生
double maxAverageScore = Double.MIN_VALUE;
String bestStudentName = "";
for (Student student : students) {
double averageScore = student.getAverageScore();
if (averageScore > maxAverageScore) {
maxAverageScore = averageScore;
bestStudentName = student.getName();
}
}
System.out.println("平均成绩最高的学生是:" + bestStudentName);
}
}
```
输出结果为:
```
姓名:张三
语文成绩:80
数学成绩:90
英语成绩:85
Java成绩:95
平均成绩:87.5
姓名:李四
语文成绩:75
数学成绩:85
英语成绩:90
Java成绩:80
平均成绩:82.5
姓名:王五
语文成绩:90
数学成绩:95
英语成绩:80
Java成绩:85
平均成绩:87.5
姓名:赵六
语文成绩:95
数学成绩:80
英语成绩:85
Java成绩:90
平均成绩:87.5
平均成绩最高的学生是:张三
阅读全文