补充Student类和StudentManage类,实现学生信息录入、排序、按学号查询学生(如果查询的学生不存在,输出提示信息), 其中,Student类的成员变量包括:String id ; //学号 String name ; //姓名int grade; //成绩,通过学号删除学生,如果要删除的学生不存在,输出提示信息,修改学生成绩功能,添加一个菜单项 “修改成绩”。请用户输入学号、新成绩,修改指定学生的成绩。如果要修改的学生不存在
时间: 2024-04-05 08:33:51 浏览: 97
好的,下面是代码实现:
Student类:
```java
public class Student implements Comparable<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;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + grade;
}
}
```
StudentManage类:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentManage {
private ArrayList<Student> studentList;
public StudentManage() {
studentList = new ArrayList<>();
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生成绩:");
int grade = scanner.nextInt();
Student student = new Student(id, name, grade);
studentList.add(student);
System.out.println("添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
studentList.remove(i);
System.out.println("删除成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("要删除的学生不存在!");
}
}
public void modifyGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改成绩的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
System.out.println("请输入新的成绩:");
int grade = scanner.nextInt();
student.setGrade(grade);
System.out.println("修改成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("要修改成绩的学生不存在!");
}
}
public void queryStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询成绩的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
System.out.println(student.toString());
flag = true;
break;
}
}
if (!flag) {
System.out.println("要查询成绩的学生不存在!");
}
}
public void sortStudent() {
Collections.sort(studentList);
System.out.println("按学号排序成功!");
for (Student student : studentList) {
System.out.println(student.toString());
}
}
public void menu() {
Scanner scanner = 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 = scanner.nextInt();
scanner.nextLine(); // 读取多余的换行符
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyGrade();
break;
case 4:
queryStudent();
break;
case 5:
sortStudent();
break;
case 0:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
public static void main(String[] args) {
StudentManage studentManage = new StudentManage();
studentManage.menu();
}
}
```
在`main`方法中创建一个`StudentManage`对象,调用`menu`方法即可开始使用学生管理系统。
阅读全文