java.输入5个学生的信息(包含学号、姓名、3科成绩),统计各学生的总分,然后将学生信息和统计结果存入二进制数据文件STUDENT.DAT中。 从第(9)题中建立的STUDENT.DAT文件中读取数据,寻找平均分最高的学生,并输出该学生的所有信息
时间: 2024-03-11 19:49:36 浏览: 67
好的,这是一个关于学生信息的问题,需要用到Java语言来编写程序。首先,我们需要定义一个学生类,包含学号、姓名和3科成绩,然后定义一个函数来读取5个学生的信息,并计算每个学生的总分,最后将学生信息和统计结果存入二进制数据文件STUDENT.DAT中。代码如下:
```java
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Student {
private String id;
private String name;
private float score1;
private float score2;
private float score3;
private float total;
public Student(String id, String name, float score1, float score2, float score3) {
this.id = id;
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.total = score1 + score2 + score3;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public float getScore1() {
return score1;
}
public float getScore2() {
return score2;
}
public float getScore3() {
return score3;
}
public float getTotal() {
return total;
}
public static void main(String[] args) {
Student[] students = readStudents();
writeStudents(students);
students = readStudentData();
Student maxStudent = findMaxAverage(students);
System.out.println("平均分最高的学生信息如下:");
System.out.println("学号:" + maxStudent.getId());
System.out.println("姓名:" + maxStudent.getName());
System.out.println("第1科成绩:" + maxStudent.getScore1());
System.out.println("第2科成绩:" + maxStudent.getScore2());
System.out.println("第3科成绩:" + maxStudent.getScore3());
System.out.println("总分:" + maxStudent.getTotal());
System.out.println("平均分:" + (maxStudent.getTotal() / 3));
}
private static Student[] readStudents() {
Student[] students = new Student[5];
for (int i = 0; i < 5; i++) {
String id = System.console().readLine("请输入第" + (i + 1) + "个学生的学号:");
String name = System.console().readLine("请输入第" + (i + 1) + "个学生的姓名:");
float score1 = Float.parseFloat(System.console().readLine("请输入第" + (i + 1) + "个学生的第1科成绩:"));
float score2 = Float.parseFloat(System.console().readLine("请输入第" + (i + 1) + "个学生的第2科成绩:"));
float score3 = Float.parseFloat(System.console().readLine("请输入第" + (i + 1) + "个学生的第3科成绩:"));
Student student = new Student(id, name, score1, score2, score3);
students[i] = student;
}
return students;
}
private static void writeStudents(Student[] students) {
try (DataOutputStream out = new DataOutputStream(new FileOutputStream("STUDENT.DAT"))) {
for (Student student : students) {
out.writeUTF(student.getId());
out.writeUTF(student.getName());
out.writeFloat(student.getScore1());
out.writeFloat(student.getScore2());
out.writeFloat(student.getScore3());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static Student[] readStudentData() {
Student[] students = new Student[5];
try {
byte[] data = Files.readAllBytes(Paths.get("STUDENT.DAT"));
for (int i = 0; i < 5; i++) {
int offset = i * 22;
String id = new String(data, offset, 5, "UTF-8").trim();
String name = new String(data, offset + 5, 20, "UTF-8").trim();
float score1 = Float.intBitsToFloat((data[offset + 25] & 0xff) << 24 | (data[offset + 24] & 0xff) << 16 | (data[offset + 23] & 0xff) << 8 | (data[offset + 22] & 0xff));
float score2 = Float.intBitsToFloat((data[offset + 29] & 0xff) << 24 | (data[offset + 28] & 0xff) << 16 | (data[offset + 27] & 0xff) << 8 | (data[offset + 26] & 0xff));
float score3 = Float.intBitsToFloat((data[offset + 33] & 0xff) << 24 | (data[offset + 32] & 0xff) << 16 | (data[offset + 31] & 0xff) << 8 | (data[offset + 30] & 0xff));
Student student = new Student(id, name, score1, score2, score3);
students[i] = student;
}
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
private static Student findMaxAverage(Student[] students) {
float maxAverage = 0;
Student maxStudent = null;
for (Student student : students) {
float average = student.getTotal() / 3;
if (average > maxAverage) {
maxAverage = average;
maxStudent = student;
}
}
return maxStudent;
}
}
```
这段代码中,我们使用了DataOutputStream来进行二进制数据的写入,并使用Files.readAllBytes和Byte数组的位运算来进行二进制数据的读取,其中22表示每个学生信息的长度。最后,我们使用findMaxAverage函数来寻找平均分最高的学生,并输出其所有信息。
阅读全文