运用java写一个学籍管理系统,要求附带学习成绩,学号,姓名,年龄,性别,可以统计全部学生总成绩,及其及格率
时间: 2024-06-11 19:06:59 浏览: 90
C++课程设计学生学籍及成绩管理系统
4星 · 用户满意度95%
由于题目比较简单,我就直接给出代码了。
```java
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String id;
private String name;
private int age;
private String gender;
private ArrayList<Integer> scores;
public Student(String id, String name, int age, String gender, ArrayList<Integer> scores) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.scores = scores;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public ArrayList<Integer> getScores() {
return scores;
}
public int getTotalScore() {
int total = 0;
for (int score : scores) {
total += score;
}
return total;
}
public double getPassRate() {
int passCount = 0;
int totalCount = scores.size();
for (int score : scores) {
if (score >= 60) {
passCount++;
}
}
return (double) passCount / totalCount;
}
}
public class StudentManager {
private ArrayList<Student> students;
public StudentManager() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(String id) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
students.remove(i);
break;
}
}
}
public void printAllStudents() {
System.out.println("学号\t姓名\t年龄\t性别\t总成绩\t及格率");
for (Student student : students) {
System.out.printf("%s\t%s\t%d\t%s\t%d\t%.2f%%\n",
student.getId(), student.getName(), student.getAge(), student.getGender(),
student.getTotalScore(), student.getPassRate() * 100);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManager manager = new StudentManager();
while (true) {
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 查看所有学生");
System.out.println("4. 退出程序");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("请输入学号:");
String id = scanner.next();
System.out.print("请输入姓名:");
String name = scanner.next();
System.out.print("请输入年龄:");
int age = scanner.nextInt();
System.out.print("请输入性别:");
String gender = scanner.next();
System.out.print("请输入成绩(以空格分隔):");
ArrayList<Integer> scores = new ArrayList<>();
while (scanner.hasNextInt()) {
scores.add(scanner.nextInt());
}
Student student = new Student(id, name, age, gender, scores);
manager.addStudent(student);
System.out.println("添加学生成功!");
} else if (choice == 2) {
System.out.print("请输入要删除的学生的学号:");
String id = scanner.next();
manager.removeStudent(id);
System.out.println("删除学生成功!");
} else if (choice == 3) {
manager.printAllStudents();
} else if (choice == 4) {
break;
} else {
System.out.println("输入无效,请重新输入!");
}
}
scanner.close();
}
}
```
这个程序实现了以下功能:
- 添加学生;
- 删除学生;
- 查看所有学生;
- 退出程序。
每个学生包含了以下信息:
- 学号;
- 姓名;
- 年龄;
- 性别;
- 成绩列表。
学生类还提供了以下方法:
- `getTotalScore()`:计算学生总成绩;
- `getPassRate()`:计算学生及格率。
学籍管理系统类提供了以下方法:
- `addStudent()`:添加学生;
- `removeStudent()`:删除学生;
- `printAllStudents()`:打印所有学生信息。
阅读全文