帮我写一个java学生成绩管理面板 ,要求有学生类(ZSStudent),成绩类(ZSGrade),管理类(ZSSrudentManagerSystem),程序运行入口:ZSStudentManagerSystem.run();
时间: 2024-02-05 17:03:06 浏览: 39
JAVA-《Java程序设计B》大作业-学生成绩管理系统
好的,我可以帮您编写一个Java学生成绩管理面板。以下是代码示例:
ZSStudent.java:
```java
public class ZSStudent {
private String name;
private int age;
private String id;
public ZSStudent(String name, int age, String id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
```
ZSGrade.java:
```java
import java.util.HashMap;
public class ZSGrade {
private HashMap<String, Double> grades;
public ZSGrade(HashMap<String, Double> grades) {
this.grades = grades;
}
public Double getGrade(String courseId) {
if (grades.containsKey(courseId)) {
return grades.get(courseId);
} else {
return 0.0;
}
}
public void setGrade(String courseId, Double score) {
grades.put(courseId, score);
}
public double getAverage() {
double sum = 0.0;
for (Double score : grades.values()) {
sum += score;
}
return sum / grades.size();
}
}
```
ZSStudentManagerSystem.java:
```java
import java.util.HashMap;
import java.util.Scanner;
public class ZSStudentManagerSystem {
private static HashMap<String, ZSGrade> gradeMap = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void run() {
while (true) {
printMenu(); // 打印菜单
int option = scanner.nextInt();
switch (option) {
case 1:
addNewStudent(); // 添加学生
break;
case 2:
addNewGrade(); // 添加成绩
break;
case 3:
printGrade(); // 显示成绩
break;
case 4:
System.out.println("谢谢使用,再见!");
return;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private static void printMenu() {
System.out.println("学生成绩管理系统");
System.out.println("-----------------------");
System.out.println("1. 添加学生");
System.out.println("2. 添加成绩");
System.out.println("3. 显示成绩");
System.out.println("4. 退出");
System.out.println("-----------------------");
System.out.println("请选择功能:");
}
private static void addNewStudent() {
System.out.println("请输入新学生的姓名:");
String name = scanner.next();
System.out.println("请输入新学生的年龄:");
int age = scanner.nextInt();
System.out.println("请输入新学生的学号:");
String id = scanner.next();
ZSStudent student = new ZSStudent(name, age, id);
gradeMap.put(id, new ZSGrade(new HashMap<String, Double>()));
System.out.println("添加学生成功!");
}
private static void addNewGrade() {
System.out.println("请输入成绩所属学生的学号:");
String studentId = scanner.next();
if (!gradeMap.containsKey(studentId)) {
System.out.println("该学生不存在!");
return;
}
System.out.println("请输入课程名称:");
String courseId = scanner.next();
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
ZSGrade grade = gradeMap.get(studentId);
grade.setGrade(courseId, score);
System.out.println("添加成绩成功!");
}
private static void printGrade() {
System.out.println("请输入要查询的学生学号:");
String studentId = scanner.next();
if (!gradeMap.containsKey(studentId)) {
System.out.println("该学生不存在!");
return;
}
ZSGrade grade = gradeMap.get(studentId);
System.out.println("学生姓名:" + grade.getName());
System.out.println("学生年龄:" + grade.getAge());
System.out.println("学生学号:" + grade.getId());
System.out.println("-----------------------");
System.out.println("| 课程名称\t| 成绩\t|");
System.out.println("-----------------------");
for (String courseId : grade.getCourseIds()) {
System.out.println("| " + courseId + "\t\t| " + grade.getGrade(courseId) + "\t|");
}
System.out.println("-----------------------");
System.out.println("平均分:" + grade.getAverage());
}
}
```
使用时,可以通过调用 `ZSStudentManagerSystem.run()` 运行程序。
阅读全文