用Java做一个班级成绩管理系统实现对班级成绩的录入、显示、增加、删除、修改、统计等操作的管理的代码
时间: 2023-11-12 11:09:45 浏览: 105
JavaWeb的学生成绩管理系统
好的,以下是一个简单的班级成绩管理系统的 Java 代码实现,包括增、删、改、查、统计等操作:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeManagementSystem {
private static List<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 查看成绩");
System.out.println("2. 录入成绩");
System.out.println("3. 修改成绩");
System.out.println("4. 删除成绩");
System.out.println("5. 统计成绩");
System.out.println("0. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 0:
System.out.println("退出系统");
System.exit(0);
case 1:
displayGrades();
break;
case 2:
addGrade();
break;
case 3:
updateGrade();
break;
case 4:
deleteGrade();
break;
case 5:
statistics();
break;
default:
System.out.println("无效操作");
}
}
}
private static void displayGrades() {
if (students.isEmpty()) {
System.out.println("暂无成绩信息");
return;
}
System.out.println("学号\t姓名\t语文\t数学\t英语");
for (Student student : students) {
System.out.println(student);
}
}
private static void addGrade() {
System.out.println("请输入学生信息(学号、姓名、语文成绩、数学成绩、英语成绩,用空格隔开):");
int id = scanner.nextInt();
String name = scanner.next();
int chinese = scanner.nextInt();
int math = scanner.nextInt();
int english = scanner.nextInt();
Student student = new Student(id, name, chinese, math, english);
students.add(student);
System.out.println("添加成功");
}
private static void updateGrade() {
System.out.println("请输入要修改的学生学号:");
int id = scanner.nextInt();
Student student = findStudent(id);
if (student == null) {
System.out.println("未找到该学生");
return;
}
System.out.println("请输入修改后的成绩(语文、数学、英语,用空格隔开):");
int chinese = scanner.nextInt();
int math = scanner.nextInt();
int english = scanner.nextInt();
student.setChinese(chinese);
student.setMath(math);
student.setEnglish(english);
System.out.println("修改成功");
}
private static void deleteGrade() {
System.out.println("请输入要删除的学生学号:");
int id = scanner.nextInt();
Student student = findStudent(id);
if (student == null) {
System.out.println("未找到该学生");
return;
}
students.remove(student);
System.out.println("删除成功");
}
private static void statistics() {
if (students.isEmpty()) {
System.out.println("暂无成绩信息");
return;
}
int sumChinese = 0;
int sumMath = 0;
int sumEnglish = 0;
double avgChinese = 0.0;
double avgMath = 0.0;
double avgEnglish = 0.0;
for (Student student : students) {
sumChinese += student.getChinese();
sumMath += student.getMath();
sumEnglish += student.getEnglish();
}
int size = students.size();
avgChinese = sumChinese / (double)size;
avgMath = sumMath / (double)size;
avgEnglish = sumEnglish / (double)size;
System.out.println("语文平均分:" + avgChinese);
System.out.println("数学平均分:" + avgMath);
System.out.println("英语平均分:" + avgEnglish);
}
private static Student findStudent(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
}
class Student {
private int id;
private String name;
private int chinese;
private int math;
private int english;
public Student(int id, String name, int chinese, int math, int english) {
this.id = id;
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return id + "\t" + name + "\t" + chinese + "\t" + math + "\t" + english;
}
}
```
以上代码实现了一个简单的班级成绩管理系统,可以通过命令行交互进行操作。
阅读全文