简要用java写一个学籍管理系统,要求附带学生成绩,学号,姓名,年龄,可以统计全部学生的总成绩,及其在班上的排名,能够统计各科的平均成绩及及格率,要求有错误提示功能,例如性别只能输入男女,输入错误提示重新输入。
时间: 2024-05-02 08:18:37 浏览: 92
java学生成绩管理系统课程设计报告.doc
以下是一个简单的学籍管理系统的示例代码,包括学生类、成绩类和管理类:
```java
import java.util.*;
class Student {
String id;
String name;
int age;
String gender;
List<Score> scores; // 一个学生可能有多门科目的成绩
public Student(String id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.scores = new ArrayList<>();
}
public void addScore(Score score) {
scores.add(score);
}
public int getTotalScore() {
int total = 0;
for (Score score : scores) {
total += score.score;
}
return total;
}
}
class Score {
String subject;
int score;
public Score(String subject, int score) {
this.subject = subject;
this.score = score;
}
}
class Management {
List<Student> students;
public Management() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void listStudents() {
System.out.println("学生列表:");
int i = 1;
for (Student student : students) {
System.out.printf("%d. %s(%s)\n", i++, student.name, student.id);
}
}
public void displayStudent(String id) {
for (Student student : students) {
if (student.id.equals(id)) {
System.out.println("学生信息:");
System.out.printf("学号:%s\n姓名:%s\n年龄:%d\n性别:%s\n", student.id, student.name, student.age, student.gender);
System.out.println("成绩:");
for (Score score : student.scores) {
System.out.printf("%s:%d\n", score.subject, score.score);
}
System.out.printf("总成绩:%d\n", student.getTotalScore());
return;
}
}
System.out.println("找不到该学生!");
}
public void listRank() {
List<Student> copy = new ArrayList<>(students); // 复制一份学生列表
copy.sort((a, b) -> b.getTotalScore() - a.getTotalScore()); // 按总成绩从高到低排序
System.out.println("学生排名:");
int i = 1;
for (Student student : copy) {
System.out.printf("%d. %s(%s) 总成绩:%d\n", i++, student.name, student.id, student.getTotalScore());
}
}
public void displayStatistics() {
Map<String, List<Integer>> subjectScores = new HashMap<>(); // 不同科目的成绩列表
for (Student student : students) {
for (Score score : student.scores) {
if (!subjectScores.containsKey(score.subject)) {
subjectScores.put(score.subject, new ArrayList<>());
}
subjectScores.get(score.subject).add(score.score);
}
}
System.out.println("各科平均成绩和及格率:");
for (Map.Entry<String, List<Integer>> entry : subjectScores.entrySet()) {
String subject = entry.getKey();
List<Integer> scores = entry.getValue();
int sum = 0;
int passCount = 0;
for (int score : scores) {
sum += score;
if (score >= 60) {
passCount++;
}
}
double average = (double) sum / scores.size();
double passRate = (double) passCount / scores.size();
System.out.printf("%s 平均成绩:%.2f,及格率:%.2f\n", subject, average, passRate);
}
}
}
public class Main {
public static void main(String[] args) {
Management management = new Management();
// 添加一些学生和成绩
Student student1 = new Student("1001", "张三", 20, "男");
student1.addScore(new Score("语文", 80));
student1.addScore(new Score("数学", 90));
student1.addScore(new Score("英语", 85));
management.addStudent(student1);
Student student2 = new Student("1002", "李四", 21, "男");
student2.addScore(new Score("语文", 70));
student2.addScore(new Score("数学", 75));
student2.addScore(new Score("英语", 80));
management.addStudent(student2);
Student student3 = new Student("1003", "王五", 19, "女");
student3.addScore(new Score("语文", 85));
student3.addScore(new Score("数学", 80));
student3.addScore(new Score("英语", 90));
management.addStudent(student3);
// 显示学生列表
management.listStudents();
// 显示某个学生的信息
management.displayStudent("1002");
// 显示学生排名
management.listRank();
// 显示各科平均成绩和及格率
management.displayStatistics();
}
}
```
在实际开发中,可以根据需求进行更详细的设计和实现,例如增加学生的删除、修改和查询功能,增加成绩的修改和查询功能等。同时还需要注意错误处理和异常处理,例如输入不合法时给出错误提示,避免程序崩溃。
阅读全文