这是初学JAVA后第一次制作简单的一个系统,或者说是一个小单元,程序略显复杂,以后还有机会的话会重构一下。
public class Students {
private String stuNum;
private String name;
private String sex;
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
if(grade>0&&grade<100)
this.grade = grade;
}
private int age;
private int grade;
}import java.util.Scanner;
//性别用man 和woman来看
public class Main {
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
System.out.println("欢迎来到XXX学生信息管理系统!");
int count = 1;
Students[] text = new Students[count];
//初始化备用数组,用于当创建新对象时将之前对象的参数赋给stus[]
title: for(int a =0;;a++){
System.out.println("1、添加学生信息");
System.out.println("2、修改学生信息");
System.out.println("3、删除学生信息");
System.out.println("4、查找学生信息");
System.out.println("5、打印学生信息");
System.out.println("6、退出");
//无限循环直到break title
Students[] stus = new Students[count];
//创建存放多个学生对象的数组,内容初始化
for(int s = 0;s<count-1;s++){
stus[s]=text[s];
//将text数组中的对象放入stus[]内
}
System.out.println("请选择操作");
switch(scan.nextInt()){
case 1:
text = new Students[count];
Students stu = new Students();
//创建单个学生对象
String stuNum=stu.getStuNum();
System.out.print("请输入学号: ");
stuNum = scan.next();
stu.setStuNum(stuNum);
String name=stu.getName();
System.out.print("请输入姓名: ");
name = scan.next();
stu.setName(name);
String sex=stu.getSex();
System.out.print("请输入性别: ");
sex = scan.next();
if(sex.equals("man")||sex.equals("woman"))
stu.setSex(sex);
else{
System.out.println("输入数据格式不正确,请重新录入数据");
continue title;
}
int age=stu.getAge();
System.out.print("请输入年龄: ");
age = scan.nextInt();
stu.setAge(age);
int grade=stu.getGrade();
System.out.print("请输入分数: ");
grade = scan.nextInt();
stu.setGrade(grade);
stus[count-1]=stu;
//将第count个学生对象存入数组stus[]中
for(int i=0;i<count;i++){
text[i]=stus[i];
//将stus[]中的所有对象放到text数组中
}
count+=1;
System.out.println("数据保存成功\n");
break ;
case 2://修改学生信息
System.out.println("请选择修改方式:");
System.out.println("1.根据姓名修改学生信息");
System.out.println("2.根据学号修改学生信息");
int c = scan.nextInt();
if(c==1){
System.out.println("请输入学生姓名:");
String na = scan.next();
for(int k = 0;k<count-1;k++){
if(stus[k].getName()==null)
continue;
else if(stus[k].getName().equals(na)){
for(int h = 0;;h++){
System.out.println(stus[k].getStuNum()+"\t"+stus[k].getName()+"\t"+stus[k].getSex()+"\t"+stus[k].getAge()+"\t"+stus[k].getGrade());
System.out.println("请选择需要修改的数据");
System.out.print("1.学号 ");
System.out.print("2.性别 ");
System.out.print("3.年龄 ");
System.out.print("4.成绩 ");
System.out.print("5.返回主菜单 ");
switch (scan.nextInt()){
case 1:
System.out.println("更改后的学号:");
String k1 = scan.next();
stus[k].setStuNum(k1);
System.out.println("修改成功");
break;
case 2:
System.out.println("更改后的性别:");
String k2 = scan.next();
stus[k].setSex(k2);
System.out.println("修改成功");
break;
case 3:
System.out.println("更改后的年龄:");
int k3 = scan.nextInt();
stus[k].setAge(k3);
System.out.println("修改成功");
break;
case 4:
System.out.println("更改后的成绩:");
int k4 = scan.nextInt();
stus[k].setGrade(k4);
System.out.println("修改成功");
break;
case 5:
continue title;
default:
System.out.println("输入数据错误");
break;
}
}
}
}
}
else if(c==2){
System.out.println("请输入学生学号:");
String sa = scan.next();
one: for(int f = 0;f<count-1;f++){
if(sa.equals(stus[f].getStuNum())){
System.out.println(stus[f].getStuNum()+"\t"+stus[f].getName()+"\t"+stus[f].getSex()+"\t"+stus[f].getAge()+"\t"+stus[f].getGrade());
while(true){
System.out.println("请选择需要修改的数据");
System.out.print("1.姓名 ");
System.out.print("2.性别 ");
System.out.print("3.年龄 ");
System.out.print("4.成绩 ");
System.out.print("5.返回主菜单 ");
int sq = scan.nextInt();
if(sq ==1){
System.out.println("更改后的姓名:");
String f1 = scan.next();
System.out.println("修改成功");
stus[f].setName(f1);
continue one;
}
else if(sq == 2){
System.out.println("更改后的性别:");
String f2 = scan.next();
System.out.println("修改成功");
stus[f].setSex(f2);
continue one;
}
else if(sq==3){
System.out.println("更改后的年龄:");
int f3 = scan.nextInt();
System.out.println("修改成功");
stus[f].setAge(f3);
continue one;
}
else if(sq==4){
System.out.println("更改后的成绩:");
int f4 = scan.nextInt();
stus[f].setGrade(f4);
System.out.println("修改成功");
continue one;
}
else if(sq==5){
continue title;
}
else{
System.out.println("输入数据错误");
continue one;
}
}
}
}
}
System.out.println("查无此人");
continue title;
case 3://删除学生信息
System.out.println("请选择删除方式:");
System.out.println("1.根据姓名删除学生信息");
System.out.println("2.根据学号删除学生信息");
int d = scan.nextInt();
if(d==1){
System.out.println("请输入学生姓名:");
String na = scan.next();
for(int q = 0;q<count-1;q++){
if(na.equals(stus[q].getName())){
for(int e =q;e<count-1;e++){
if(e<count-2){
stus[e].setGrade(stus[e+1].getGrade());
stus[e].setAge(stus[e+1].getAge());
stus[e].setName(stus[e+1].getName());
stus[e].setStuNum(stus[e+1].getStuNum());
stus[e].setSex(stus[e+1].getSex());
}
}
count--;
System.out.println("删除成功!!");
continue title;
}
}
System.out.println("查无此人");
continue title;
}
else if(d==2){
System.out.println("请输入学生学号");
String st = scan.next();
for(int g = 0;g<count-1;g++){
if(st.equals(stus[g].getStuNum())){
for(int j=g;j<count-1;j++){
if(j<count-2){
stus[j].setGrade(stus[j+1].getGrade());
stus[j].setAge(stus[j+1].getAge());
stus[j].setName(stus[j+1].getName());
stus[j].setStuNum(stus[j+1].getStuNum());
stus[j].setSex(stus[j+1].getSex());
}
}
count--;
System.out.println("删除成功!!");
continue title;
}
}
System.out.println("查无此人");
continue title;
}
else{
System.out.println("没有此选项,返回主菜单");
continue title;
}
case 4:
System.out.println("请选择查找方式:");
System.out.println("1.根据姓名查找学生信息");
System.out.println("2.根据学号查找学生信息");
int b = scan.nextInt();
if(b==1){
System.out.println("请输入学生姓名:");
String nam = scan.next();
for(int u = 0;u<count-1;u++){
if(nam.equals(stus[u].getName())){
System.out.println("您要查找的学生信息为:");
System.out.println("学号\t姓名\t性别\t年龄\t分数");
System.out.println(stus[u].getStuNum()+"\t"+stus[u].getName()+"\t"+stus[u].getSex()+"\t"+stus[u].getAge()+"\t"+stus[u].getGrade());
System.out.println();
continue title;
}
}
System.out.println("没有此学生");
continue title;
}
else if(b==2){
System.out.println("请输入学生学号:");
String st = scan.next();
for(int u = 0;u<count-1;u++){
if(st.equals(stus[u].getStuNum())){
System.out.println("您要查找的学生信息为:");
System.out.println("学号\t\t姓名\t\t性别\t年龄\t分数");
System.out.println(stus[u].getStuNum()+"\t"+stus[u].getName()+"\t"+stus[u].getSex()+"\t"+stus[u].getAge()+"\t"+stus[u].getGrade());
System.out.println();
continue title;
}
}
System.out.println("没有此学生");
continue title;
}
else{
System.out.println("没有此选项,返回主菜单");
continue title;
}
case 5:
System.out.println("学号\t\t姓名\t\t性别\t年龄\t分数");
for(int i=0;i<count-1;i++){
System.out.println();
System.out.println(stus[i].getStuNum()+"\t"+stus[i].getName()+"\t"+stus[i].getSex()+"\t"+stus[i].getAge()+"\t"+stus[i].getGrade());
}
break;
case 6:
System.out.println("欢迎再次使用,再见!!!");
break title;
default:
break;
}
}
}
}