用java编写一个成绩管理系统
时间: 2023-09-19 14:07:32 浏览: 72
用Java编写成绩管理系统
4星 · 用户满意度95%
抱歉,作为AI语言模型,我无法编写完整的Java程序。但我可以给您提供一个简单的Java类作为参考,它可以实现一个简单的成绩管理系统。
```
import java.util.ArrayList;
import java.util.Scanner;
public class GradeManagementSystem {
private ArrayList<Student> students;
public GradeManagementSystem() {
students = new ArrayList<Student>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(int index) {
students.remove(index);
}
public void displayStudents() {
for (int i = 0; i < students.size(); i++) {
System.out.println((i+1) + ". " + students.get(i).toString());
}
}
public void displayStudent(int index) {
System.out.println(students.get(index).toString());
}
public void updateStudent(int index, Student student) {
students.set(index, student);
}
public void calculateAverage() {
double sum = 0;
for (int i = 0; i < students.size(); i++) {
sum += students.get(i).getGrade();
}
double average = sum / students.size();
System.out.println("Average grade: " + average);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
GradeManagementSystem system = new GradeManagementSystem();
while (true) {
System.out.println("1. Add student");
System.out.println("2. Remove student");
System.out.println("3. Display all students");
System.out.println("4. Display student");
System.out.println("5. Update student");
System.out.println("6. Calculate average grade");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter student name: ");
String name = scanner.next();
System.out.print("Enter student grade: ");
double grade = scanner.nextDouble();
Student student = new Student(name, grade);
system.addStudent(student);
break;
case 2:
System.out.print("Enter student index: ");
int index = scanner.nextInt() - 1;
system.removeStudent(index);
break;
case 3:
system.displayStudents();
break;
case 4:
System.out.print("Enter student index: ");
index = scanner.nextInt() - 1;
system.displayStudent(index);
break;
case 5:
System.out.print("Enter student index: ");
index = scanner.nextInt() - 1;
System.out.print("Enter student name: ");
name = scanner.next();
System.out.print("Enter student grade: ");
grade = scanner.nextDouble();
student = new Student(name, grade);
system.updateStudent(index, student);
break;
case 6:
system.calculateAverage();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}
class Student {
private String name;
private double grade;
public Student(String name, double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
public void setName(String name) {
this.name = name;
}
public void setGrade(double grade) {
this.grade = grade;
}
public String toString() {
return "Name: " + name + ", Grade: " + grade;
}
}
```
该程序实现了以下功能:
- 添加学生
- 删除学生
- 显示所有学生
- 显示单个学生
- 更新学生信息
- 计算平均成绩
在程序运行时,用户可以输入数字来选择所需的功能。程序使用一个 `ArrayList` 来存储学生对象,并使用一个 `Student` 类来表示学生。
阅读全文