public class Student {

private String no; //学号(长度为6位)

private String name; //姓名

private String idCard; //证件号(18位)

private int sex; //性别(男(1)/女(0))

private int age; //年龄(1-100)

public Student(){}

public Student(String no, String name, String idCard, int sex, int age) {

super();

this.no = no;

this.name = name;

this.idCard = idCard;

this.sex = sex;

this.age = age;

}

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getIdCard() {

return idCard;

}

public void setIdCard(String idCard) {

this.idCard = idCard;

}

public int getSex() {

return sex;

}

public void setSex(int sex) {

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [no=" + no + ", name=" + name + ", idCard=" + idCard + ", sex=" + sex + ", age=" + age

  • "]";

}

}

import java.util.Scanner;

public class StuMgr3_obj {

static int count = 0; //当前学生

static int N = 1000; //可添加的最大学生人数

static Scanner input = new Scanner(System.in);

static Student[] stu = new Student[N];

public static void main(String[] args) {

while(true) {

StuMgr3_obj.showMenu();

System.out.print("请输入<0-5>选择:");

int sel = input.nextInt();

if(sel < 0 || sel > 5) {

System.out.println("没有此项菜单,请重新选择");

continue;

}

switch (sel) {

case 1:

StuMgr3_obj.addStu();

break;

case 2:

StuMgr3_obj.delStu();

break;

case 3:

StuMgr3_obj.updateStu();

break;

case 4:

StuMgr3_obj.selectStu();

break;

case 5:

StuMgr3_obj.selectAllStu();

break;

case 0:

StuMgr3_obj.exitSys();

break;

}

}

}

/**

  • 菜单

*/

public static void showMenu() {

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("| 0. 退出学生系统 |");

System.out.println("---------------------------------");

}

/**

*1.增加学生信息

*/

public static void addStu() {

System.out.print("请输入学号(一定是6位):");

String no;

while(true) {

no = input.next();

if(6 != no.length()) {

System.out.println("请重新输入学号:");

continue;

}

break;

}

System.out.println("请输入姓名");

String name = input.next();

String idCard;

while (true) {

System.out.println("请输入证件号(一定是18位):");

idCard = input.next();

if (18 != idCard.length()) {

System.out.println("请重新输入证件号");

}else

break;

}

System.out.println("请输入性别(男(1)/女(0))");

int sex = input.nextInt();

System.out.println("请输入年龄(1-100)");

int age = input.nextInt();

stu[count] = new Student(no,name,idCard,sex,age);

count++;

}

/**

  • 2.删除学生信息

*/

public static void delStu() {

System.out.print("请输入要删除学生的学号:");

String noString = input.next();

int index = getIndex(noString);

if (index == -1) {

System.out.println("系统中不存在该学生");

}else {

stu[index] = stu[count-1];

System.out.println("已删除成功");

count--;

}

}

/**

  • 3.修改学生信息

*/

public static void updateStu() {

System.out.print("请输入要修改学生的学号:");

String noString = input.next();

int index = getIndex(noString);

if (index == -1) {

System.out.println("系统中不存在该学生");

}else {

System.out.println("请输入学号:");

stu[index].setNo(input.next());

System.out.println("请重新输入姓名:");

stu[index].setName(input.next());

System.out.println("请重新输入证件号:");

stu[index].setIdCard(input.next());

System.out.println("请重新输入性别:");

stu[index].setSex(input.nextInt());

System.out.println("请重新输入年龄(1-100):");

stu[index].setAge(input.nextInt());

}

}

/**

XM

*4.查询学生信息

*/

public static void selectStu() {

System.out.print("请输入想查询学生的学号:");

String noString = input.next();

int index = getIndex(noString);

if (index == -1) {

System.out.println("系统中不存在该学生");

}else {

System.out.println("学号:"+stu[index].getNo());

System.out.println("姓名:"+stu[index].getName());

System.out.println("证件号:"+stu[index].getIdCard());

System.out.println("性别((男(1)/女(0)):"+stu[index].getSex());

System.out.println("年龄:"+stu[index].getAge());

}

}

/**

  • 5.查询所有学生信息

*/

public static void selectAllStu() {

for (int i = 0; i < count; i++) {

System.out.println("姓名为"+stu[i].getName()+"的学生信息为:");

System.out.println("学号:"+stu[i].getNo());

System.out.println("证件号:"+stu[i].getIdCard());

System.out.println("性别(男(1)/女(0)):"+stu[i].getSex());

System.out.println("年龄:"+stu[i].getAge());

System.out.println();

}

}

/**

  • 0.退出学生系统

*/

public static void exitSys() {

System.out.println("已退出程序");

System.exit(1);

}

/**

  • @param noString 学号

  • @return 返回该学生在数组中的索引,不存在该学生则返回-1

*/

public static int getIndex(String noString) {

for (int i = 0; i < count; i++) {

if (stu[i].getNo().equals(noString)) {

return i;

}

}

return -1;

}

可实现增、删查看学生信息的功能。