开发一个简单的java学生成绩管理系统,实现以下基础功能: 1. **学生信息录入**:能够录入学生的基本信息(学号、姓名)以及一门课程的成绩。 2. **成绩计算与展示**:计算学生的成绩,并展示所有学生的信息和成绩。 3. **文件操作**:将学生信息和成绩保存到文件中,同时能从文件中读取数据。 4. **异常处理**:在录入信息和文件操作时,对可能出现的异常(如文件读写错误)进行处理。
时间: 2025-05-20 21:27:47 浏览: 40
### 使用Java实现学生成绩管理系统
以下是基于提供的引用内容和专业知识构建的一个完整的解决方案,涵盖了学生信息录入、成绩计算与展示、文件保存与读取以及异常处理的功能。
#### 功能概述
该系统实现了以下核心功能:
- **录入学生信息**:支持输入学生的姓名和成绩[^1]。
- **显示所有学生信息**:以表格形式展示已录入的学生数据[^1]。
- **查询学生成绩**:根据学生姓名查找对应的成绩[^1]。
- **统计功能**:提供班级总成绩、平均成绩、最高分和最低分的计算[^1]。
- **文件操作**:将学生数据存储到本地文件并能够重新加载[^2]。
- **异常处理**:捕获可能发生的错误(如非法输入或文件访问失败),提高程序健壮性。
---
#### 代码实现
下面是一个简化版的 `StudentManagementSystem` 类及其辅助类:
```java
import java.io.*;
import java.util.*;
class Student {
private String name;
private double score;
public Student(String name, double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
public class StudentManagementSystem {
private List<Student> students = new ArrayList<>();
// 添加学生信息
public void addStudent(Student student) throws Exception {
if (student.getName().isEmpty()) {
throw new IllegalArgumentException("学生姓名不能为空");
}
if (student.getScore() < 0 || student.getScore() > 100) {
throw new IllegalArgumentException("分数应在0至100之间");
}
students.add(student);
}
// 显示所有学生信息
public void displayAllStudents() {
System.out.println("\n当前学生列表:");
System.out.printf("%-15s%-15s\n", "姓名", "成绩");
for (Student s : students) {
System.out.printf("%-15s%-15.2f\n", s.getName(), s.getScore());
}
}
// 查询特定学生的信息
public Optional<Double> queryScoreByName(String name) {
return students.stream()
.filter(s -> s.getName().equalsIgnoreCase(name))
.map(Student::getScore)
.findFirst();
}
// 统计功能
public Map<String, Double> calculateStatistics() {
double totalScore = 0;
double maxScore = Double.MIN_VALUE;
double minScore = Double.MAX_VALUE;
for (Student s : students) {
totalScore += s.getScore();
if (s.getScore() > maxScore) maxScore = s.getScore();
if (s.getScore() < minScore) minScore = s.getScore();
}
double averageScore = students.size() > 0 ? totalScore / students.size() : 0;
Map<String, Double> stats = new HashMap<>();
stats.put("totalScore", totalScore);
stats.put("averageScore", averageScore);
stats.put("maxScore", maxScore);
stats.put("minScore", minScore);
return stats;
}
// 文件保存
public void saveToFile(String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Student s : students) {
writer.write(s.getName() + "," + s.getScore());
writer.newLine();
}
}
}
// 文件读取
public void loadFromFile(String filePath) throws IOException {
students.clear(); // 清空现有数据
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
try {
students.add(new Student(parts[0], Double.parseDouble(parts[1])));
} catch (NumberFormatException e) {
System.err.println("跳过无效记录:" + line);
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
int choice;
do {
System.out.println("\n菜单:");
System.out.println("1. 录入学生成绩");
System.out.println("2. 查看所有学生信息");
System.out.println("3. 查询某位学生的成绩");
System.out.println("4. 计算统计数据");
System.out.println("5. 保存数据到文件");
System.out.println("6. 加载数据从文件");
System.out.println("7. 退出");
System.out.print("请选择(1-7): ");
choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
System.out.print("请输入学生姓名: ");
String name = scanner.nextLine();
System.out.print("请输入学生成绩: ");
double score = Double.parseDouble(scanner.nextLine());
try {
system.addStudent(new Student(name, score));
System.out.println("成功添加学生!");
} catch (Exception e) {
System.err.println(e.getMessage());
}
break;
case 2:
system.displayAllStudents();
break;
case 3:
System.out.print("请输入要查询的学生姓名: ");
String searchName = scanner.nextLine();
Optional<Double> result = system.queryScoreByName(searchName);
if (result.isPresent()) {
System.out.println("找到成绩: " + result.get());
} else {
System.out.println("未找到该学生!");
}
break;
case 4:
Map<String, Double> stats = system.calculateStatistics();
System.out.println("总成绩: " + stats.get("totalScore"));
System.out.println("平均成绩: " + stats.get("averageScore"));
System.out.println("最高分: " + stats.get("maxScore"));
System.out.println("最低分: " + stats.get("minScore"));
break;
case 5:
try {
system.saveToFile("students.txt");
System.out.println("数据已保存到文件.");
} catch (IOException e) {
System.err.println("无法写入文件: " + e.getMessage());
}
break;
case 6:
try {
system.loadFromFile("students.txt");
System.out.println("数据已从文件加载.");
} catch (IOException e) {
System.err.println("无法读取文件: " + e.getMessage());
}
break;
case 7:
System.out.println("感谢使用本系统。再见!");
break;
default:
System.out.println("无效选项,请重试!");
}
} while (choice != 7);
scanner.close();
}
}
```
---
#### 关键点解析
1. **学生对象封装**
定义了 `Student` 类来表示单个学生的数据结构,包括姓名和成绩属性[^1]。
2. **异常处理机制**
在 `addStudent` 方法中加入了对非法输入的校验逻辑,防止用户输入空白名字或超出范围的成绩[^1]。
3. **文件存取能力**
利用了标准库中的 `FileReader` 和 `FileWriter` 来完成持久化存储的需求[^2]。
4. **交互式菜单驱动**
提供了一个循环式的命令行界面让用户可以方便地执行各种操作[^1]。
---
阅读全文