用java写一个学籍管理系统,要求附带学生成绩,学号,姓名,年龄,可以统计全部学生的总成绩,及其在班上的排名,能够统计各 科的平均成绩及及指率,要求有错误是示功能,
时间: 2024-06-04 16:11:11 浏览: 89
xueshengguanli.rar_学生_学生成绩管理_学籍管理_学籍管理 学生学籍管理_学籍管理系统
以下是一个简单的学籍管理系统的Java代码,包含学生成绩、学号、姓名、年龄等信息,并可以统计总成绩、排名、各科平均成绩和指率,并具备错误提示功能:
import java.util.*;
public class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<Student>();
}
public void addStudent(Student s) {
students.add(s);
}
public void removeStudent(String id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getID().equals(id)) {
students.remove(i);
return;
}
}
System.out.println("Error: No student with ID " + id + " found.");
}
public Student findStudent(String id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getID().equals(id)) {
return students.get(i);
}
}
System.out.println("Error: No student with ID " + id + " found.");
return null;
}
public void listStudents() {
System.out.println("ID\tName\tAge\tTotal Score\tRank");
Collections.sort(students);
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).getID() + "\t" + students.get(i).getName() + "\t" + students.get(i).getAge() + "\t" + students.get(i).getTotalScore() + "\t\t" + (i + 1));
}
}
public double getAverageScore(String course) {
double sum = 0;
int count = 0;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getScore(course) >= 0) {
sum += students.get(i).getScore(course);
count++;
}
}
return (count == 0) ? 0 : (sum / count);
}
public double getPassRate(String course) {
int passCount = 0;
int totalCount = 0;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getScore(course) >= 0) {
totalCount++;
if (students.get(i).getScore(course) >= 60) {
passCount++;
}
}
}
return (totalCount == 0) ? 0 : ((double) passCount / totalCount);
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
// Add some students
sms.addStudent(new Student("1001", "Alice", 18));
sms.addStudent(new Student("1002", "Bob", 19));
sms.addStudent(new Student("1003", "Charlie", 20));
// Add some scores
sms.findStudent("1001").addScore("Math", 80);
sms.findStudent("1001").addScore("English", 85);
sms.findStudent("1002").addScore("Math", 70);
sms.findStudent("1002").addScore("English", 75);
sms.findStudent("1003").addScore("Math", 60);
sms.findStudent("1003").addScore("English", 65);
// List all students
sms.listStudents();
// Get average and pass rate for Math
System.out.println("Math Average Score: " + sms.getAverageScore("Math"));
System.out.println("Math Pass Rate: " + sms.getPassRate("Math"));
// Remove a student
sms.removeStudent("1002");
// List all students again
sms.listStudents();
}
}
class Student implements Comparable<Student> {
private String id;
private String name;
private int age;
private HashMap<String, Integer> scores;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
scores = new HashMap<String, Integer>();
}
public void addScore(String course, int score) {
scores.put(course, score);
}
public int getScore(String course) {
if (scores.containsKey(course)) {
return scores.get(course);
} else {
System.out.println("Error: No score for course " + course + ".");
return -1;
}
}
public int getTotalScore() {
int sum = 0;
for (int score : scores.values()) {
sum += score;
}
return sum;
}
public String getID() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compareTo(Student s) {
return s.getTotalScore() - this.getTotalScore();
}
}
阅读全文