使用java编写:有若干个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,并按从大到小的顺序进行排序,排序后数据以表格形式输出。再创建一个泛型管理类,从键盘一波一波输入数据,其中包括四波人(小学生,初中生,高中生,大学生)的数据信息,要使其能增加能删除相关学生信息。并使用接口来实现。
时间: 2024-01-02 20:02:26 浏览: 28
以下是使用 Java 编写的代码,包括输入学生成绩、计算平均成绩、按照平均成绩排序并输出表格,以及泛型管理类实现增删功能的部分。其中,四波人的数据信息可以使用不同的类来表示,然后使用接口来统一管理。
```
import java.util.*;
// 学生类
class Student implements Comparable<Student> {
private int id;
private String name;
private double[] scores;
private double average;
public Student(int id, String name, double[] scores) {
this.id = id;
this.name = name;
this.scores = scores;
this.average = (scores[0] + scores[1] + scores[2]) / 3;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double[] getScores() {
return scores;
}
public double getAverage() {
return average;
}
// 按照平均成绩从大到小排序
public int compareTo(Student other) {
if (this.average > other.average) {
return -1;
} else if (this.average < other.average) {
return 1;
} else {
return 0;
}
}
}
// 泛型管理类
interface StudentManager<T> {
public void add(T student);
public void remove(T student);
public void display();
}
class StudentList<T extends Student> implements StudentManager<T> {
private List<T> list;
public StudentList() {
this.list = new ArrayList<T>();
}
// 增加学生
public void add(T student) {
this.list.add(student);
}
// 删除学生
public void remove(T student) {
this.list.remove(student);
}
// 显示学生列表
public void display() {
System.out.println("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均成绩");
for (T student : this.list) {
System.out.printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", student.getId(), student.getName(),
student.getScores()[0], student.getScores()[1], student.getScores()[2], student.getAverage());
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
// 输入学生成绩
while (true) {
System.out.print("请输入学生学号(输入-1结束):");
int id = scanner.nextInt();
if (id == -1) {
break;
}
System.out.print("请输入学生姓名:");
String name = scanner.next();
double[] scores = new double[3];
for (int i = 0; i < 3; i++) {
System.out.printf("请输入第%d门课的成绩:", i + 1);
scores[i] = scanner.nextDouble();
}
Student student = new Student(id, name, scores);
students.add(student);
}
// 按照平均成绩排序
Collections.sort(students);
// 输出表格
System.out.println("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均成绩");
for (Student student : students) {
System.out.printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", student.getId(), student.getName(),
student.getScores()[0], student.getScores()[1], student.getScores()[2], student.getAverage());
}
// 使用泛型管理类
StudentList<Student> studentList = new StudentList<>();
studentList.add(new Student(1, "小学生1", new double[]{60, 70, 80}));
studentList.add(new Student(2, "小学生2", new double[]{70, 80, 90}));
studentList.add(new Student(3, "初中生1", new double[]{80, 90, 100}));
studentList.add(new Student(4, "初中生2", new double[]{90, 100, 80}));
studentList.add(new Student(5, "高中生1", new double[]{100, 80, 90}));
studentList.add(new Student(6, "高中生2", new double[]{80, 90, 100}));
studentList.add(new Student(7, "大学生1", new double[]{90, 100, 80}));
studentList.add(new Student(8, "大学生2", new double[]{100, 80, 90}));
studentList.remove(students.get(0)); // 删除第一个学生
studentList.display(); // 显示学生列表
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)