写一个Java的代码,关于学生成绩系统的删除学生信息,成绩信息的代码
时间: 2023-05-29 11:08:02 浏览: 107
以下是一个简单的学生成绩系统的Java代码,包括删除学生信息和成绩信息的功能。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentGradeSystem {
static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 添加学生成绩");
System.out.println("4. 删除学生成绩");
System.out.println("5. 查看学生成绩");
System.out.println("0. 退出系统");
System.out.println("请选择功能:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
addGrade();
break;
case 4:
deleteGrade();
break;
case 5:
viewGrade();
break;
case 0:
System.out.println("谢谢使用!");
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
} while (choice != 0);
scanner.close();
}
private static void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
students.add(new Student(name, id));
System.out.println("添加成功!");
}
private static void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生!");
}
private static void addGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入科目名称:");
String subject = scanner.nextLine();
System.out.println("请输入成绩:");
double grade = scanner.nextDouble();
student.addGrade(subject, grade);
System.out.println("成绩添加成功!");
return;
}
}
System.out.println("未找到该学生!");
}
private static void deleteGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入要删除的科目名称:");
String subject = scanner.nextLine();
if (student.deleteGrade(subject)) {
System.out.println("成绩删除成功!");
} else {
System.out.println("未找到该科目成绩!");
}
return;
}
}
System.out.println("未找到该学生!");
}
private static void viewGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
student.printGrades();
return;
}
}
System.out.println("未找到该学生!");
}
}
class Student {
private String name;
private String id;
private ArrayList<Grade> grades = new ArrayList<>();
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public void addGrade(String subject, double grade) {
grades.add(new Grade(subject, grade));
}
public boolean deleteGrade(String subject) {
for (Grade grade : grades) {
if (grade.getSubject().equals(subject)) {
grades.remove(grade);
return true;
}
}
return false;
}
public void printGrades() {
System.out.println("学号:" + id + ",姓名:" + name + ",成绩如下:");
for (Grade grade : grades) {
System.out.println(grade.getSubject() + ":" + grade.getGrade());
}
}
}
class Grade {
private String subject;
private double grade;
public Grade(String subject, double grade) {
this.subject = subject;
this.grade = grade;
}
public String getSubject() {
return subject;
}
public double getGrade() {
return grade;
}
}
```
阅读全文