java实现学生成绩管理
时间: 2023-08-25 13:05:49 浏览: 79
用java实现学生成绩管理
以下是一个简单的Java学生成绩管理程序:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentGradeManager {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
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. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(students, scanner);
break;
case 2:
deleteStudent(students, scanner);
break;
case 3:
updateStudent(students, scanner);
break;
case 4:
queryStudent(students, scanner);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private static void addStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生学号:");
String id = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别:");
String gender = scanner.next();
System.out.println("请输入学生成绩:");
double grade = scanner.nextDouble();
Student student = new Student(name, id, age, gender, grade);
students.add(student);
System.out.println("添加成功!");
}
private static void deleteStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要删除的学生的学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该学号的学生!");
}
private static void updateStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要修改的学生的学号:");
String id = scanner.next();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入修改后的学生姓名:");
String name = scanner.next();
System.out.println("请输入修改后的学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入修改后的学生性别:");
String gender = scanner.next();
System.out.println("请输入修改后的学生成绩:");
double grade = scanner.nextDouble();
student.setName(name);
student.setAge(age);
student.setGender(gender);
student.setGrade(grade);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该学号的学生!");
}
private static void queryStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要查询的学生的学号:");
String id = scanner.next();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("没有找到该学号的学生!");
}
}
class Student {
private String name;
private String id;
private int age;
private String gender;
private double grade;
public Student(String name, String id, int age, String gender, double grade) {
this.name = name;
this.id = id;
this.age = age;
this.gender = gender;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
@Override
public String toString() {
return "学生信息:" +
"姓名:" + name +
", 学号:" + id +
", 年龄:" + age +
", 性别:" + gender +
", 成绩:" + grade;
}
}
```
该程序包含以下功能:
1. 添加学生信息:包括姓名、学号、年龄、性别、成绩;
2. 删除学生信息:根据学号删除学生信息;
3. 修改学生信息:根据学号修改学生的姓名、年龄、性别、成绩;
4. 查询学生信息:根据学号查询学生的所有信息;
5. 退出程序。
程序使用了一个`Student`类来表示学生信息,并使用了一个`ArrayList`来存储所有学生的信息。在程序运行时,用户可以根据菜单选择需要进行的操作。每个操作都会调用相应的函数来完成。
阅读全文