编写一个模拟学生成绩管理系统的程序,要求如下:
你是想实现这样的效果吗
这个岂不更直观~
// Student类
public class Student {
private int id;
private String name;
private double score;
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getInfo() {
return "ID: " + id + "\nName: " + name + "\nScore: " + score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
// ScoreManager类
public class ScoreManager {
private Student[] students;
private int count;
public ScoreManager(int size) {
students = new Student[size];
count = 0;
}
public void addStudent(Student student) {
students[count++] = student;
}
public void removeStudent(int id) {
for (int i = 0; i < count; i++) {
if (students[i].getId() == id) {
for (int j = i; j < count-1; j++) {
students[j] = students[j+1];
}
students[--count] = null;
break;
}
}
}
public double getAverageScore() {
double sum = 0;
for (int i = 0; i < count; i++) {
sum += students[i].getScore();
}
return sum / count;
}
public Student getStudentById(int id) {
for (int i = 0; i < count; i++) {
if (students[i].getId() == id) {
return students[i];
}
}
return null; // 未找到对应的学生
}
}
// Main类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ScoreManager scoreManager = new ScoreManager(10);
Student s1 = new Student(1, "张三", 80.5);
Student s2 = new Student(2, "李四", 90.0);
Student s3 = new Student(3, "王五", 75.5);
scoreManager.addStudent(s1);
scoreManager.addStudent(s2);
scoreManager.addStudent(s3);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:\n1、添加学生\n2、删除学生\n3、查找学生\n4、显示学生信息\n5、显示平均成绩\n6、退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生ID:");
int id = scanner.nextInt();
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生成绩:");
double score = scanner.nextDouble();
Student newStudent = new Student(id, name, score);
scoreManager.addStudent(newStudent);
break;
case 2:
System.out.println("请输入要删除学生的ID:");
int deleteId = scanner.nextInt();
scoreManager.removeStudent(deleteId);
break;
case 3:
System.out.println("请输入要查找学生的ID:");
int searchId = scanner.nextInt();
Student student = scoreManager.getStudentById(searchId);
if (student != null) {
System.out.println(student.getInfo());
} else {
System.out.println("未找到对应的学生!");
}
break;
case 4:
for (int i = 0; i < scoreManager.count; i++) {
System.out.println(scoreManager.students[i].getInfo());
}
break;
case 5:
double averageScore = scoreManager.getAverageScore();
System.out.println("平均成绩为:" + averageScore + "分。");
break;
case 6:
System.exit(0);
default:
System.out.println("无效操作,请重新选择。");
break;
}
}
}
}
class Student {
private int id;
private String name;
private double score;
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getInfo() {
return "ID: " + id + ", Name: " + name + ", Score: " + score;
}
public int getId() {
return id;
}
}
class ScoreManager {
private Student[] students;
private int count;
public ScoreManager(int capacity) {
students = new Student[capacity];
count = 0;
}
public void addStudent(Student student) {
if (count < students.length) {
students[count] = student;
count++;
System.out.println("Successfully added student: " + student.getInfo());
} else {
System.out.println("Failed to add student. The student list is full.");
}
}
public void removeStudent(int id) {
for (int i = 0; i < count; i++) {
if (students[i].getId() == id) {
System.out.println("Successfully removed student: " + students[i].getInfo());
students[i] = students[count - 1];
students[count - 1] = null;
count--;
return;
}
}
System.out.println("Failed to remove student. Student with ID " + id + " not found.");
}
public double getAverageScore() {
double sum = 0;
for (int i = 0; i < count; i++) {
sum += students[i].getScore();
}
return sum / count;
}
public Student getStudentById(int id) {
for (int i = 0; i < count; i++) {
if (students[i].getId() == id) {
return students[i];
}
}
return null;
}
}
public class Main {
public static void main(String[] args) {
// 实例化学生对象
Student student1 = new Student(1, "Alice", 80.0);
Student student2 = new Student(2, "Bob", 75.5);
Student student3 = new Student(3, "Charlie", 90.5);
// 实例化成绩管理系统对象
ScoreManager scoreManager = new ScoreManager(5);
// 添加学生
scoreManager.addStudent(student1);
scoreManager.addStudent(student2);
scoreManager.addStudent(student3);
// 打印学生信息
System.out.println("Student Information:");
for (int i = 0; i < scoreManager.getCount(); i++) {
System.out.println(scoreManager.getStudents()[i].getInfo());
}
// 删除学生
scoreManager.removeStudent(2);
// 打印学生信息
System.out.println("Updated Student Information:");
for (int i = 0; i < scoreManager.getCount(); i++) {
System.out.println(scoreManager.getStudents()[i].getInfo());
}
// 打印平均成绩
System.out.println("Average Score: " + scoreManager.getAverageScore());
}
}