编写一个类Zong1.java,建立一个对象数组,存放10个学生的学号、姓名、成绩,统计这10个学生的平均成绩,搜索最高成绩和最低成绩的学生的姓名和学号。
时间: 2024-05-14 21:16:25 浏览: 12
以下是Zong1.java的代码实现:
```java
public class Zong1 {
public static void main(String[] args) {
Student[] students = new Student[10];
students[0] = new Student("001", "张三", 80);
students[1] = new Student("002", "李四", 85);
students[2] = new Student("003", "王五", 90);
students[3] = new Student("004", "赵六", 95);
students[4] = new Student("005", "钱七", 78);
students[5] = new Student("006", "孙八", 82);
students[6] = new Student("007", "周九", 88);
students[7] = new Student("008", "吴十", 92);
students[8] = new Student("009", "郑十一", 87);
students[9] = new Student("010", "陈十二", 91);
double sum = 0;
double avg;
double maxScore = students[0].getScore();
double minScore = students[0].getScore();
String maxName = students[0].getName();
String minName = students[0].getName();
String maxId = students[0].getId();
String minId = students[0].getId();
for (int i = 0; i < students.length; i++) {
sum += students[i].getScore();
if (students[i].getScore() > maxScore) {
maxScore = students[i].getScore();
maxName = students[i].getName();
maxId = students[i].getId();
}
if (students[i].getScore() < minScore) {
minScore = students[i].getScore();
minName = students[i].getName();
minId = students[i].getId();
}
}
avg = sum / students.length;
System.out.println("平均成绩:" + avg);
System.out.println("最高成绩学生:" + maxName + ",学号:" + maxId + ",成绩:" + maxScore);
System.out.println("最低成绩学生:" + minName + ",学号:" + minId + ",成绩:" + minScore);
}
}
class Student {
private String id;
private String name;
private double score;
public Student(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
```
输出结果:
```
平均成绩:86.6
最高成绩学生:赵六,学号:004,成绩:95.0
最低成绩学生:钱七,学号:005,成绩:78.0
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)