采取控制台的方式去书写学生管理系统。
"-------------欢迎进入学生管理系统----------------"
"1:添加学生"
"2:删除学生"
"3:修改学生"
"4:查询学生"
"5:退出"
"请输入您的选择:"
属性:id、姓名、年龄、家庭住址
键盘录入每一个学生信息并添加,需要满足以下要求:
键盘录入要删除的学生id,需要满足以下要求:
键盘录入要修改的学生id,需要满足以下要求
打印所有的学生信息,需要满足以下要求
id 姓名 年龄 家庭住址
2023001 张三 23 南京
2023002 李四 24 北京
2023003 王五 25 广州
2023004 赵六 26 深圳
要初始化学生管理系统的主界面,可以按照以下思路进行操作:
下面是一个示例的代码片段,演示了如何初始化学生管理系统的主界面:
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 5) {
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.print("请输入您的选择: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
// 添加学生逻辑
break;
case 2:
// 删除学生逻辑
break;
case 3:
// 修改学生逻辑
break;
case 4:
// 查询学生逻辑
break;
case 5:
System.out.println("感谢使用学生管理系统,再见!");
break;
default:
System.out.println("无效的选择,请重新输入!");
break;
}
System.out.println(); // 输出空行,增加可读性
}
scanner.close();
}
}
在这个示例中,我们首先创建了一个StudentManagementSystem对象system,用于管理学生信息。然后,在一个无限循环中,打印主界面菜单供用户选择操作。根据用户的选择,我们调用相应的学生管理系统方法进行操作。当用户选择退出时,我们使用System.exit(0)结束程序的执行。
学生类所含属性:id、姓名、年龄、家庭住址
要编写学生类,可以按照以下思路进行操作:
下面是一个示例的学生类代码片段,演示了如何编写学生类:
public class Student {
private int id;
private String name;
private int age;
private String address;
public Student(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
// Getter and Setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生ID:");
int id = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
System.out.println("请输入学生家庭住址:");
String address = scanner.nextLine();
Student student = new Student(id, name, age, address);
students.add(student);
System.out.println("学生添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生ID:");
int id = scanner.nextInt();
boolean found = false;
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
found = true;
System.out.println("学生删除成功!");
break;
}
}
if (!found) {
System.out.println("未找到匹配的学生ID!");
}
}
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生ID:");
int id = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
boolean found = false;
for (Student student : students) {
if (student.getId() == id) {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
student.setName(name);
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
student.setAge(age);
System.out.println("请输入学生家庭住址:");
String address = scanner.nextLine();
student.setAddress(address);
found = true;
System.out.println("学生修改成功!");
break;
}
}
if (!found) {
System.out.println("未找到匹配的学生ID!");
}
}
public void displayStudents() {
if (students.isEmpty()) {
System.out.println("当前无学生信息,请添加后再查询!");
} else {
System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
for (Student student : students) {
System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getAddress());
}
}
}
package net.army.java.test;
/**
* 功能:学生管理系统
* 日期:2023年09月02日
* 作者:梁辰兴
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生ID:");
int id = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
System.out.println("请输入学生家庭住址:");
String address = scanner.nextLine();
Student student = new Student(id, name, age, address);
students.add(student);
System.out.println("学生添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生ID:");
int id = scanner.nextInt();
boolean found = false;
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
found = true;
System.out.println("学生删除成功!");
break;
}
}
if (!found) {
System.out.println("未找到匹配的学生ID!");
}
}
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生ID:");
int id = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
boolean found = false;
for (Student student : students) {
if (student.getId() == id) {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
student.setName(name);
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 消费掉换行符
student.setAge(age);
System.out.println("请输入学生家庭住址:");
String address = scanner.nextLine();
student.setAddress(address);
found = true;
System.out.println("学生修改成功!");
break;
}
}
if (!found) {
System.out.println("未找到匹配的学生ID!");
}
}
public void displayStudents() {
if (students.isEmpty()) {
System.out.println("当前无学生信息,请添加后再查询!");
} else {
System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
for (Student student : students) {
System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getAddress());
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
// 菜单
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("请输入选择:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.deleteStudent();
break;
case 3:
system.updateStudent();
break;
case 4:
system.displayStudents();
break;
case 5:
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("无效的选择,请重新输入!");
break;
}
}
}
}