j ava学生管理系统代码
时间: 2023-10-16 17:11:02 浏览: 122
基于ava后台资金管理系统设计与实现.docx
5星 · 资源好评率100%
以下是一个简单的 Java 学生管理系统代码示例:
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
static ArrayList<Student> studentList = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
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();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
System.exit(0);
default:
System.out.println("无效的选项,请重新输入!");
break;
}
}
}
public static void addStudent() {
System.out.println("添加学生");
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生性别:");
String gender = scanner.next();
System.out.print("请输入学生班级:");
String className = scanner.next();
Student student = new Student(name, age, gender, className);
studentList.add(student);
System.out.println("添加成功!");
}
public static void deleteStudent() {
System.out.println("删除学生");
System.out.print("请输入要删除的学生姓名:");
String name = scanner.next();
boolean deleted = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getName().equals(name)) {
studentList.remove(i);
deleted = true;
System.out.println("删除成功!");
break;
}
}
if (!deleted) {
System.out.println("没有找到该学生!");
}
}
public static void modifyStudent() {
System.out.println("修改学生");
System.out.print("请输入要修改的学生姓名:");
String name = scanner.next();
boolean modified = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getName().equals(name)) {
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生性别:");
String gender = scanner.next();
System.out.print("请输入学生班级:");
String className = scanner.next();
student.setAge(age);
student.setGender(gender);
student.setClassName(className);
modified = true;
System.out.println("修改成功!");
break;
}
}
if (!modified) {
System.out.println("没有找到该学生!");
}
}
public static void queryStudent() {
System.out.println("查询学生");
System.out.print("请输入要查询的学生姓名:");
String name = scanner.next();
boolean found = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getName().equals(name)) {
System.out.println(student.toString());
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到该学生!");
}
}
}
class Student {
private String name;
private int age;
private String gender;
private String className;
public Student(String name, int age, String gender, String className) {
this.name = name;
this.age = age;
this.gender = gender;
this.className = className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",班级:" + className;
}
}
```
这个程序实现了以下功能:
- 添加学生
- 删除学生
- 修改学生
- 查询学生
你可以根据需要进行修改和扩展。
阅读全文