用java定义包含学号、姓名和成绩信息的学生类Student。在测试类中定义存放n个学生对象存放到某个集合对象中(具体集合自行确定),然后按成绩进行降序排列,并将排序结果(类Student对象)保存到文件stud.dat中,再读出文件stud.dat中所有的学生信息并显示。
时间: 2024-02-29 15:53:03 浏览: 22
好的,我会尽力解答。以下是Java代码实现:
Student类:
```java
public class Student implements Comparable<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 void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
return Double.compare(o.score, this.score); // 按成绩降序排列
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + score;
}
}
```
测试类:
```java
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Student> students = new ArrayList<>(); // 存放学生对象的集合
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生总数:");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("请输入学号:");
String id = scanner.next();
System.out.print("请输入姓名:");
String name = scanner.next();
System.out.print("请输入成绩:");
double score = scanner.nextDouble();
students.add(new Student(id, name, score));
}
Collections.sort(students); // 按成绩降序排列
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stud.dat"));
for (Student student : students) {
oos.writeObject(student); // 写入文件
}
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stud.dat"));
while (true) {
try {
Student student = (Student) ois.readObject(); // 读出文件
System.out.println(student);
} catch (EOFException e) {
break;
}
}
ois.close();
}
}
```
在控制台输入学生信息后,程序会自动按照成绩降序排列,并将结果保存到文件“stud.dat”中。然后读出文件中所有学生信息并显示在控制台上。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)