java有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
时间: 2023-12-20 18:48:00 浏览: 128
这是一个编程问题,可以使用Java语言编写程序来解决。首先定义一个学生类,包含学号、姓名和三门课程成绩三个属性,然后输入五个学生的数据,计算每个学生的平均分数,并将原有数据和平均分数存储到磁盘文件中。
以下是示例代码:
```
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Student {
String id;
String name;
double score1;
double score2;
double score3;
double averageScore;
public Student(String id, String name, double score1, double score2, double score3) {
this.id = id;
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.averageScore = (score1 + score2 + score3) / 3;
}
public String toString() {
return id + "\t" + name + "\t" + score1 + "\t" + score2 + "\t" + score3 + "\t" + averageScore;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[5];
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的信息(学号 姓名 三门课成绩):");
String id = scanner.next();
String name = scanner.next();
double score1 = scanner.nextDouble();
double score2 = scanner.nextDouble();
double score3 = scanner.nextDouble();
students[i] = new Student(id, name, score1, score2, score3);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("stud"));
writer.write("学号\t姓名\t成绩1\t成绩2\t成绩3\t平均成绩\n");
for (Student student : students) {
writer.write(student.toString() + "\n");
}
writer.flush();
writer.close();
System.out.println("保存成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文