学生成绩管理系统java,200行代码
时间: 2023-12-22 22:18:16 浏览: 53
学生成绩管理系统的代码
5星 · 资源好评率100%
好的,下面是一个不到200行的Java实现,包括了学生类、学生管理类和主函数:
1. 学生类
```java
public class Student {
private String id;
private String name;
private int grade;
public Student(String id, String name, int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + grade;
}
}
```
2. 学生管理类
```java
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
private List<Student> students;
public StudentManager() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public Student findStudentById(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public List<Student> findStudentByName(String name) {
List<Student> result = new ArrayList<>();
for (Student student : students) {
if (student.getName().equals(name)) {
result.add(student);
}
}
return result;
}
public double getAverageGrade() {
double sum = 0.0;
for (Student student : students) {
sum += student.getGrade();
}
return sum / students.size();
}
public List<Student> getAllStudents() {
return students;
}
}
```
3. 主函数
```java
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManager manager = new StudentManager();
System.out.println("欢迎使用学生成绩管理系统!");
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("6. 计算平均分");
System.out.println("0. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学号、姓名和成绩:");
String id = scanner.next();
String name = scanner.next();
int grade = scanner.nextInt();
Student student = new Student(id, name, grade);
manager.addStudent(student);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的学生的学号:");
String idToDelete = scanner.next();
Student studentToDelete = manager.findStudentById(idToDelete);
if (studentToDelete == null) {
System.out.println("该学生不存在!");
} else {
manager.removeStudent(studentToDelete);
System.out.println("删除成功!");
}
break;
case 3:
System.out.println("请输入要查询的学生的学号:");
String idToFind = scanner.next();
Student studentToFind = manager.findStudentById(idToFind);
if (studentToFind == null) {
System.out.println("该学生不存在!");
} else {
System.out.println(studentToFind);
}
break;
case 4:
System.out.println("请输入要查询的学生的姓名:");
String nameToFind = scanner.next();
List<Student> students = manager.findStudentByName(nameToFind);
if (students.isEmpty()) {
System.out.println("该学生不存在!");
} else {
for (Student s : students) {
System.out.println(s);
}
}
break;
case 5:
List<Student> allStudents = manager.getAllStudents();
if (allStudents.isEmpty()) {
System.out.println("暂无学生信息!");
} else {
for (Student s : allStudents) {
System.out.println(s);
}
}
break;
case 6:
double avgGrade = manager.getAverageGrade();
System.out.println("平均分:" + avgGrade);
break;
case 0:
System.out.println("感谢使用学生成绩管理系统!");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入!");
}
}
}
}
```
以上代码实现了一个简单的学生成绩管理系统,包括添加学生、删除学生、查询学生、统计平均分等功能。注意,该实现仅供参考,您可以根据自己的需求进行修改和完善。
阅读全文