package bean; import java.util.Scanner; public class StudentManag { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { while (true) { System.out.println("欢迎进入学生信息管理系统\n 1.添加学生信息 \n 2.修改学生信息 \n 3.查询学生信息 \n 4.删除学生信息 \n 5.退出 \n 请输入序号选择相应的管理功能:"); Scanner scanner = new Scanner(System.in); int code = scanner.nextInt(); if (code == 1) { addStudent(); System.out.println("学生信息添加成功!"); continue; } else if (code == 2) { } else if (code == 3) { } else if (code == 4) { } else if (code == 5) { break; } else { System.out.println("请输入1到5之间的整数"); } } } //添加学生的方法 public static void addStudent() { Student student = new Student(); System.out.println("请输入学号"); String id = scanner.next(); student.setSid(id); System.out.println("请输入姓名"); String name = scanner.next(); student.setSname(name); System.out.println("请输入性别"); char sex = scanner.next().charAt(0); student.setSex(sex); System.out.println("请输入年龄"); student.getAge(scanner.nextInt()); System.out.println("请输入班级"); student.setStuclass(scanner.next()); //把使用读入数据封装好的对象添加到数据集合中 BaseDate.studentData.put(student.getSid(), student); }集合的方法写修改删除查询
时间: 2024-02-14 16:09:48 浏览: 151
Einleser:java.util.Scanner 的示例
//修改学生信息的方法
public static void modifyStudent() {
System.out.println("请输入要修改学生的学号:");
String id = scanner.next();
Student student = BaseDate.studentData.get(id);
if (student == null) {
System.out.println("没有找到该学生!");
return;
}
System.out.println("请输入修改后的姓名:");
String name = scanner.next();
student.setSname(name);
System.out.println("请输入修改后的性别:");
char sex = scanner.next().charAt(0);
student.setSex(sex);
System.out.println("请输入修改后的年龄:");
int age = scanner.nextInt();
student.setAge(age);
System.out.println("请输入修改后的班级:");
String stuclass = scanner.next();
student.setStuclass(stuclass);
System.out.println("学生信息修改成功!");
}
//查询学生信息的方法
public static void queryStudent() {
System.out.println("请输入要查询的学生的学号:");
String id = scanner.next();
Student student = BaseDate.studentData.get(id);
if (student == null) {
System.out.println("没有找到该学生!");
return;
}
System.out.println("学号:" + student.getSid() + ",姓名:" + student.getSname() + ",性别:" + student.getSex() + ",年龄:" + student.getAge() + ",班级:" + student.getStuclass());
}
//删除学生信息的方法
public static void deleteStudent() {
System.out.println("请输入要删除的学生的学号:");
String id = scanner.next();
Student student = BaseDate.studentData.get(id);
if (student == null) {
System.out.println("没有找到该学生!");
return;
}
BaseDate.studentData.remove(id);
System.out.println("学生信息删除成功!");
}
阅读全文