使用Java编写一个学生管理系统,需要包括添加,排序,查询,修改,删除学生信息的功能,学生的信息包括学号,年龄,性别,数学与语文与英语的成绩以及它的平均分
时间: 2023-10-22 18:04:56 浏览: 77
Java学生管理系统(控制台)实现了学生姓名,学号,居住地,年龄的添加,删除,修改,查询。解决了学号的重复问题,不存在的问题等
5星 · 资源好评率100%
好的,这是一个比较复杂的问题。首先,你需要定义一个学生类,包括学号、年龄、性别、数学、语文、英语成绩以及平均分等属性。可以按照以下方式定义:
```
public class Student {
private String id;
private int age;
private String gender;
private double mathScore;
private double chineseScore;
private double englishScore;
private double averageScore;
// 构造函数
public Student(String id, int age, String gender, double mathScore, double chineseScore, double englishScore, double averageScore) {
this.id = id;
this.age = age;
this.gender = gender;
this.mathScore = mathScore;
this.chineseScore = chineseScore;
this.englishScore = englishScore;
this.averageScore = averageScore;
}
// getter、setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getChineseScore() {
return chineseScore;
}
public void setChineseScore(double chineseScore) {
this.chineseScore = chineseScore;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getAverageScore() {
return averageScore;
}
public void setAverageScore(double averageScore) {
this.averageScore = averageScore;
}
}
```
接下来,你需要编写一个学生管理类,包含添加、排序、查询、修改和删除学生信息的方法。可以按照以下方式定义:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
private List<Student> studentList = new ArrayList<Student>();
// 添加学生信息
public void addStudent() {
Scanner input = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("学号:");
String id = input.next();
System.out.print("年龄:");
int age = input.nextInt();
System.out.print("性别:");
String gender = input.next();
System.out.print("数学成绩:");
double mathScore = input.nextDouble();
System.out.print("语文成绩:");
double chineseScore = input.nextDouble();
System.out.print("英语成绩:");
double englishScore = input.nextDouble();
double averageScore = (mathScore + chineseScore + englishScore) / 3; // 计算平均分
Student student = new Student(id, age, gender, mathScore, chineseScore, englishScore, averageScore);
studentList.add(student);
System.out.println("添加成功!");
}
// 排序
public void sortStudent() {
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.getAverageScore() > o2.getAverageScore()) {
return -1;
} else if (o1.getAverageScore() < o2.getAverageScore()) {
return 1;
} else {
return 0;
}
}
});
System.out.println("排序成功!");
}
// 查询学生信息
public void queryStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查询的学生学号:");
String id = input.next();
for (Student student : studentList) {
if (id.equals(student.getId())) {
System.out.println("学号\t年龄\t性别\t数学成绩\t语文成绩\t英语成绩\t平均分");
System.out.println(student.getId() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getMathScore() + "\t" + student.getChineseScore() + "\t" + student.getEnglishScore() + "\t" + student.getAverageScore());
return;
}
}
System.out.println("未找到该学生!");
}
// 修改学生信息
public void modifyStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = input.next();
for (Student student : studentList) {
if (id.equals(student.getId())) {
System.out.println("请输入修改后的学生信息:");
System.out.print("年龄:");
int age = input.nextInt();
System.out.print("性别:");
String gender = input.next();
System.out.print("数学成绩:");
double mathScore = input.nextDouble();
System.out.print("语文成绩:");
double chineseScore = input.nextDouble();
System.out.print("英语成绩:");
double englishScore = input.nextDouble();
double averageScore = (mathScore + chineseScore + englishScore) / 3; // 计算平均分
student.setAge(age);
student.setGender(gender);
student.setMathScore(mathScore);
student.setChineseScore(chineseScore);
student.setEnglishScore(englishScore);
student.setAverageScore(averageScore);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该学生!");
}
// 删除学生信息
public void deleteStudent() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = input.next();
for (Student student : studentList) {
if (id.equals(student.getId())) {
studentList.remove(student);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生!");
}
}
```
最后,你可以编写一个主函数来测试学生管理系统的功能:
```
public class Main {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
Scanner input = new Scanner(System.in);
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 = input.nextInt();
switch (choice) {
case 1:
studentManager.addStudent();
break;
case 2:
studentManager.sortStudent();
break;
case 3:
studentManager.queryStudent();
break;
case 4:
studentManager.modifyStudent();
break;
case 5:
studentManager.deleteStudent();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("无效的选择!");
break;
}
}
}
}
```
希望这个代码可以帮助你完成学生管理系统的编写。
阅读全文