package cn.edu.ahau.mgc.stu; public class Student { private String id; private String name; private int age; public String getId() { return id; } public void setId(String 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; } } StudentAction.java:学生信息处理类 package cn.edu.ahau.mgc.stu; import java.util.Iterator; import java.util.Scanner; import java.util.Vector; public class StudentAction { private Vector<Student> stus = new Vector<Student>(); Scanner sc = new Scanner(System.in); public void add() { Student stu = new Student(); System.out.println("请输入学生学号:"); String id = sc.next(); System.out.println("请输入学生姓名:"); String name = sc.next(); System.out.println("请输入学生年龄:"); int age = sc.nextInt(); stu.setId(id); stu.setName(name); stu.setAge(age); this.stus.add(stu); } public void showAll() { Iterator<Student> iter = this.stus.iterator(); this.printHeader(); while (iter.hasNext()) { Student stu = iter.next(); this.print(stu); } this.printFooter(); } public void printHeader() { System.out.println("------------------------"); System.out.println("学号/t姓名/t年龄"); System.out.println("------------------------"); } public void printFooter() { System.out.println("------------------------"); } public void print(Student stu) { System.out.println(stu.getId() + "/t" + stu.getName() + "/t" + stu.getAge()); } public Student queryById() { String id = sc.next(); Student stu = null; Iterator<Student> iter = this.stus.iterator(); while (iter.hasNext()) { Student queryStu = iter.next(); if (id != null && id.equals(queryStu.getId())) { stu = queryStu; break; } } if (stu != null) { this.printHeader(); this.print(stu); this.printFooter(); } else { System.out.println("对不起,没找到您要查找的学生!"); } return stu; } } StudentMgr.java:学生信息管理类 package cn.edu.ahau.mgc.stu; import java.util.Scanner; public class StudentMgr { public static void main(String[] args) { StudentAction sa = new StudentAction(); Scanner sc = new Scanner(System.in); boolean flag = true; while (flag) { showMenu(); int selected = sc.nextInt(); switch(selected) { case 1 : sa.add(); break; case 2 : sa.showAll(); break; case 3 : sa.queryById(); break; case 4 : flag = false; System.out.println("已退出!"); break; } } } public static void showMenu() { System.out.println("学生信息管理菜单:"); System.out.println("(1)增加学生信息"); System.out.println("(2)显示所有学生信息"); System.out.println("(3)根据学号查询学生信息"); System.out.println("(4)退出管理"); } }