用java实现以下内容:某班有30名学生,每个学生有学号,姓名和4门成绩(学生信息随机产生,存入一个对象数组)输入这名学生的学号,姓名,成绩(4门课成绩均小于100分,以空格区分),然后显示这名学生平均分,最高分,全班的最高平均分学生信息,新学生成绩是否高于全班平均分,
时间: 2024-01-04 19:03:08 浏览: 36
以下是实现代码:
```java
import java.util.*;
public class Student {
private String id;
private String name;
private int[] scores;
public Student(String id, String name, int[] scores) {
this.id = id;
this.name = name;
this.scores = scores;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int[] getScores() {
return scores;
}
public double getAverageScore() {
int sum = 0;
for (int score : scores) {
sum += score;
}
return (double) sum / scores.length;
}
public int getHighestScore() {
int highest = scores[0];
for (int score : scores) {
if (score > highest) {
highest = score;
}
}
return highest;
}
public static Student getHighestAverageScoreStudent(Student[] students) {
Student highestAverageScoreStudent = students[0];
for (Student student : students) {
if (student.getAverageScore() > highestAverageScoreStudent.getAverageScore()) {
highestAverageScoreStudent = student;
}
}
return highestAverageScoreStudent;
}
public static double getClassAverageScore(Student[] students) {
double sum = 0;
for (Student student : students) {
sum += student.getAverageScore();
}
return sum / students.length;
}
public static void main(String[] args) {
Student[] students = new Student[30];
Random random = new Random();
for (int i = 0; i < students.length; i++) {
String id = String.format("%04d", i + 1);
String name = "Student" + id;
int[] scores = new int[4];
for (int j = 0; j < scores.length; j++) {
scores[j] = random.nextInt(100) + 1;
}
students[i] = new Student(id, name, scores);
}
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生的学号、姓名和成绩(以空格分隔):");
String input = scanner.nextLine();
String[] parts = input.split("\\s+");
String id = parts[0];
String name = parts[1];
int[] scores = new int[4];
for (int i = 0; i < scores.length; i++) {
scores[i] = Integer.parseInt(parts[2 + i]);
}
Student student = new Student(id, name, scores);
System.out.println("该名学生的平均分为:" + student.getAverageScore());
System.out.println("该名学生的最高分为:" + student.getHighestScore());
Student highestAverageScoreStudent = getHighestAverageScoreStudent(students);
System.out.println("全班最高平均分学生的信息为:");
System.out.println("学号:" + highestAverageScoreStudent.getId() + ",姓名:" + highestAverageScoreStudent.getName() + ",平均分:" + highestAverageScoreStudent.getAverageScore());
double classAverageScore = getClassAverageScore(students);
System.out.println("全班平均分为:" + classAverageScore);
if (student.getAverageScore() > classAverageScore) {
System.out.println("新学生成绩高于全班平均分。");
} else {
System.out.println("新学生成绩不高于全班平均分。");
}
}
}
```
代码思路:
1. 定义 `Student` 类,包含学号、姓名、成绩等属性和相关方法。
2. 随机生成 30 名学生的信息,存入 `Student` 对象数组 `students` 中。
3. 通过控制台输入一名学生的信息,创建 `Student` 对象 `student`。
4. 分别调用 `Student` 对象的方法计算平均分、最高分,并输出。
5. 定义 `getHighestAverageScoreStudent` 和 `getClassAverageScore` 静态方法,分别计算全班最高平均分学生和全班平均分。
6. 调用 `getHighestAverageScoreStudent` 方法获取全班最高平均分学生的信息,并输出。
7. 输出全班平均分。
8. 判断新学生成绩是否高于全班平均分,并输出结果。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)