用java中的io流文件设计一个简单的学生成绩系统,具有以下功能:添加一个学生;查找一个学生;根据编号更新学生的基本信息(基本信息为姓名与学号);根据编号删除学生;根据编号输入学生的各门成绩(语文、数学、外语,成绩为int类型);退出系统。
时间: 2023-11-22 18:52:28 浏览: 114
好的,这是一个非常典型的文件IO案例,下面是代码实现:
```java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentScoreSystem {
private static final String FILE_NAME = "students.txt"; // 数据文件名
public static void main(String[] args) {
System.out.println("欢迎使用学生成绩管理系统!");
Scanner scanner = new Scanner(System.in);
ArrayList<Student> students = loadStudents(); // 读入数据文件中的学生信息
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 添加学生");
System.out.println("2. 查找学生");
System.out.println("3. 更新学生信息");
System.out.println("4. 删除学生");
System.out.println("5. 输入学生的各门成绩");
System.out.println("0. 退出系统");
int choice = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
switch (choice) {
case 1:
addStudent(scanner, students);
break;
case 2:
searchStudent(scanner, students);
break;
case 3:
updateStudent(scanner, students);
break;
case 4:
removeStudent(scanner, students);
break;
case 5:
inputScores(scanner, students);
break;
case 0:
saveStudents(students); // 保存学生信息到数据文件
System.out.println("谢谢使用!");
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 从数据文件中读入学生信息
private static ArrayList<Student> loadStudents() {
ArrayList<Student> students = new ArrayList<>();
File file = new File(FILE_NAME);
if (!file.exists()) {
return students; // 如果文件不存在,返回空列表
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
Student student = new Student(parts[0], parts[1], Integer.parseInt(parts[2]), 0, 0, 0);
students.add(student);
} else if (parts.length == 6) {
Student student = new Student(parts[0], parts[1], Integer.parseInt(parts[2]),
Integer.parseInt(parts[3]), Integer.parseInt(parts[4]), Integer.parseInt(parts[5]));
students.add(student);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
// 将学生信息保存到数据文件
private static void saveStudents(ArrayList<Student> students) {
try (FileWriter writer = new FileWriter(FILE_NAME)) {
for (Student student : students) {
if (student.getChineseScore() == 0 && student.getMathScore() == 0 && student.getEnglishScore() == 0) {
writer.write(student.getName() + "," + student.getId() + "," + student.getAge() + "\n");
} else {
writer.write(student.getName() + "," + student.getId() + "," + student.getAge() + ","
+ student.getChineseScore() + "," + student.getMathScore() + "," + student.getEnglishScore()
+ "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 添加一个学生
private static void addStudent(Scanner scanner, ArrayList<Student> students) {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入学号:");
int id = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
System.out.print("请输入年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
Student student = new Student(name, id, age, 0, 0, 0);
students.add(student);
System.out.println("添加成功!");
}
// 查找一个学生
private static void searchStudent(Scanner scanner, ArrayList<Student> students) {
System.out.print("请输入要查找的学生的学号:");
int id = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
for (Student student : students) {
if (student.getId() == id) {
System.out.println(student);
return;
}
}
System.out.println("未找到该学生!");
}
// 更新学生信息
private static void updateStudent(Scanner scanner, ArrayList<Student> students) {
System.out.print("请输入要更新的学生的学号:");
int id = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
for (Student student : students) {
if (student.getId() == id) {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
student.setName(name);
System.out.println("更新成功!");
return;
}
}
System.out.println("未找到该学生!");
}
// 删除一个学生
private static void removeStudent(Scanner scanner, ArrayList<Student> students) {
System.out.print("请输入要删除的学生的学号:");
int id = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生!");
}
// 输入学生的各门成绩
private static void inputScores(Scanner scanner, ArrayList<Student> students) {
System.out.print("请输入要输入成绩的学生的学号:");
int id = scanner.nextInt();
scanner.nextLine(); // 把回车吃掉
for (Student student : students) {
if (student.getId() == id) {
System.out.print("请输入语文成绩:");
int chineseScore = scanner.nextInt();
student.setChineseScore(chineseScore);
System.out.print("请输入数学成绩:");
int mathScore = scanner.nextInt();
student.setMathScore(mathScore);
System.out.print("请输入英语成绩:");
int englishScore = scanner.nextInt();
student.setEnglishScore(englishScore);
System.out.println("成绩输入成功!");
return;
}
}
System.out.println("未找到该学生!");
}
}
class Student {
private String name;
private int id;
private int age;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int id, int age, int chineseScore, int mathScore, int englishScore) {
this.name = name;
this.id = id;
this.age = age;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public Student(String name, int id, int age) {
this(name, id, age, 0, 0, 0);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
@Override
public String toString() {
return "姓名:" + name + ",学号:" + id + ",年龄:" + age + ",语文成绩:" + chineseScore + ",数学成绩:"
+ mathScore + ",英语成绩:" + englishScore;
}
}
```
这个程序使用了Java的文件IO,将学生信息保存到文件中。每个学生的信息包括姓名、学号、年龄和三门成绩。程序中实现了添加学生、查找学生、更新学生信息、删除学生、输入学生的各门成绩等功能。
阅读全文