文章目录
- 案例一
- 案例二
- 案例三
- 案例四
- 案例五
案例一
Student.Java
package curd;public class Student {private String stuid;private String name;private int chinese;private int math;private int english;private int avg;private int sum;public int getAvg() {return avg;}public void setAvg(int avg) {this.avg = avg;}public int getSum() {return sum;}public void setSum(int sum) {this.sum = sum;}public String getStuid() {return stuid;}public void setStuid(String stuid) {this.stuid = stuid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public String getMessage() {String messages="学生编号 :"+this.stuid+" 学生姓名: "+this.name+" 语文:"+this.chinese+" 数学:"+this.math+" 英语: "+this.english+" 总分 :"+this.sum+" 平均分 :"+this.avg;return messages;}public Student(String stuid,String name,int chinese,int math, int english) {this.chinese=chinese;this.english=english;this.math=math;this.name=name;this.stuid=stuid;this.sum=chinese+math+english;this.avg=this.sum/3;}public Student() {}}
StudentAll.Java
package curd;public class StudentAll{public static Student[] save(Student student,Student [] students) {boolean fal=checkSid(student.getStuid(), students);//先查询是否有这个 学生编号if(fal) {System.out.println("该学生编号已存在,请重新输入");return students;}for(int i=0;i<students.length;i++) {if(students[i]==null) {students[i]=student;break;}}return students;}public static String getStudent(String stuid,Student [] students) {Student s=null;for (Student student : students) {if(student!=null&&student.getStuid().equals(stuid)) {s=student;}}String messages=s==null?"error: 该编号不存在请重新输入":s.getMessage();return messages;}public static Student[] update (String stuid,Student student,Student [] students) {boolean fal=checkSid(stuid, students);//先查询是否有这个 学生编号if(!fal) {System.out.println("请输入正确的学生编号");return students;}for(int i=0;i<students.length;i++) {if(students[i]!=null&&students[i].getStuid().equals(stuid)) {students[i]=student;}}return students;}public static Student[] delStudent(String stuid,Student [] students) {boolean fal=checkSid(stuid, students);//先查询是否有这个 学生编号if(!fal) {System.out.println("请输入正确的学生编号");return students;}for(int i=0;i<students.length;i++) {if(students[i]!=null&&students[i].getStuid().equals(stuid)) {students[i]=null;}}return students;}public static void getStudeltAll(Student [] students) {for (Student student : students) {if(student!=null) {System.out.println(student.getMessage());}}}//检验学生编号是否存在方法public static boolean checkSid(String stuid,Student [] students) {boolean fal=false;String messages=getStudent(stuid, students);//先查询是否有这个 学生编号if(!messages.contains("error")) {//判断是否包含 error 有则没有此学生编号fal=true;}return fal;}}
StudentMenu.Java
package curd;import java.util.Scanner;public class StudentMenu {public static void main(String[] args) {Scanner scn = new Scanner(System.in); System.out.print("请输入学生总人数");int count= scn.nextInt();Student [] students=new Student [count];while (true) {System.out.println("1.新增一名学生2");System.out.println("2.根据学号查询学生");System.out.println("3.根据学号修改学生");System.out.println("4. 根据学号删除一名学生");System.out.println("5. 查看所有学生");System.out.println(" 其他.退出");int x= scn.nextInt();switch(x) {case 1 : {System.out.println("请输入学生编号");String stuid=scn.next();System.out.println("请输入学生姓名");String name=scn.next();System.out.println("请输入该学生语文成绩");int chinese= scn.nextInt();System.out.println("请输入该学生数学成绩");int math= scn.nextInt();System.out.println("请输入该学生英语成绩");int english= scn.nextInt();Student student =new Student(stuid, name, chinese, math, english);students=StudentAll.save(student, students);break;}case 2:{System.out.println("请输入学生编号");String stuid=scn.next();System.out.println(StudentAll.getStudent(stuid, students));break;}case 3:{System.out.println("请输入要修改的学生编号");String stuid=scn.next();System.out.println("请输入学生姓名");String name=scn.next();System.out.println("请输入该学生语文成绩");int chinese= scn.nextInt();System.out.println("请输入该学生数学成绩");int math= scn.nextInt();System.out.println("请输入该学生英语成绩");int english= scn.nextInt();Student student =new Student(stuid, name, chinese, math, english);students=StudentAll.update(stuid, student, students);break;}case 4:{System.out.println("请输入学生编号");String stuid=scn.next();students=StudentAll.delStudent(stuid, students);break;}case 5:{StudentAll.getStudeltAll(students);break;}default:{System.exit(0);}}}}
}
案例二
Student.java
package stu_demo;public class Student {private String name;private String id;private boolean sex;private int age;private int score;static String school;public Student(){}public Student(String name,String id,boolean sex,int age,int score) {this.name=name;this.id=id;this.sex=sex;this.age=age;this.score=score;}public String getName(){return name;}public void setName(String name) {this.name=name;}public String getId() {return id;}public void setId(String id) {this.id=id;}public boolean getSex() {return sex;}public void setSex(boolean sex) {this.sex=sex;}public int getAge() {return age;}public void setAge(int age) {this.age=age;}public int getScore() {return score;}public void setScore(int score) {this.score=score;}
}
StudentMenu.java
package stu_demo;import java.util.Scanner;
import java.util.ArrayList;
public class StudentMenu {public static void main(String[] args) {Scanner input=new Scanner(System.in);ArrayList<Student> stu=new ArrayList<Student>();int select=1;String zhucename="";String zhucemima="";String yemian="yes";while(yemian.equals("yes")) {System.out.println("欢迎使用OLALA学生管理系统");System.out.println("1.注册");System.out.println("2.登录");System.out.println("请选择以下操作");int shuru=input.nextInt();if(shuru==1) {System.out.println("------------注册页面------------");System.out.println("姓名");String name_1=input.next();System.out.println("密码");String mima_1=input.next();zhucename=name_1;zhucemima=mima_1;System.out.println("注册成功");}else if(shuru==2) {System.out.println("------------登录页面------------");System.out.println("姓名");String name_2=input.next();System.out.println("密码");String mima_2=input.next();if(name_2.equals(zhucename)&&mima_2.equals(zhucemima)){yemian="no";System.out.println("登陆成功,欢迎"+zhucename);for(;;) {yemian();do {select=input.nextInt();}while(select<1||select>5);switch(select){case 1:addStudent(stu);break;case 2:deleteStudent(stu);break;case 3:updateStudent(stu);break;case 4:selectStudent(stu);break;case 5:showStudent(stu);break;}}}} }}private static void yemian() {System.out.println("***************************************************");System.out.println(" OLALA学生信息管理系统");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("***************************************************");}public static void addStudent(ArrayList<Student>stu) {Student s=new Student();String name,id,f;int age,score;boolean sex=false,y=true;Scanner input=new Scanner(System.in);System.out.println("请依次输入:姓名、学号、年龄、性别、成绩");System.out.println("姓名");name=input.next();System.out.println("学号");id=input.next();System.out.println("年龄");age=input.nextInt();System.out.println("性别(0为男,非0为女)");f=input.next();System.out.println("成绩");score=input.nextInt();if(stu.size()!=0){for(int i=0;i<stu.size();i++){if(stu.get(i).getId().equals(id)){y=false;}}}if(y==true) {if(f.equals("0")){sex=false;}else{sex=true;}s.setName(name);s.setId(id);s.setSex(sex);s.setAge(age);s.setScore(score);stu.add(s);}else {System.out.println("学号重复!该学生已存在!");}}public static void deleteStudent(ArrayList<Student>stu) {boolean f=false;String str;System.out.println("请输入要删除的学生的学号:");Scanner input=new Scanner(System.in);str=input.next();for(int i=0;i<stu.size();i++){if(stu.get(i).getId().equals(str)){f=true;stu.remove(i);break;}}if(f==false){System.out.println("该学生不存在!");}}public static void updateStudent(ArrayList<Student>stu) {Student s;String name, id,f;int age,score,num=0;boolean sex,j=true;Scanner input=new Scanner(System.in);System.out.println("请输入待修改学生学号:");id=input.next();if(stu.size()!=0){for(int i=0;i<stu.size();i++){if(stu.get(i).getId().equals(id)) {j=false;}}}if(j==true){System.out.println("该学生不存在!");}else{for(int i=0;i<stu.size();i++){if(stu.get(i).getId().equals(id)){num=i;break;}}System.out.println("请输入新的信息:");System.out.println("修改后的名字");id=input.next();System.out.println("修改后的学号");name=input.next();System.out.println("修改后的年龄");age=input.nextInt();System.out.println("修改后的性别");f=input.next();System.out.println("修改后的成绩");score=input.nextInt();if(f=="0"){sex=false;} else{sex=true;}stu.get(num).setName(name);stu.get(num).setId(id);stu.get(num).setSex(sex);stu.get(num).setAge(age);stu.get(num).setScore(score); }}public static void selectStudent(ArrayList<Student>stu) {boolean j=true;//默认无重复int num=0;String s;Scanner input=new Scanner(System.in);String str;System.out.println("请输入学号:");str=input.next();if(stu.size()!=0){for(int i=0;i<stu.size();i++){if(stu.get(i).getId().equals(str)){j=false;}}}if(j==true){System.out.println("该学生不存在!");}else {for(int i=0;i<stu.size();i++) {if(stu.get(i).getId().equals(str)) {num=i;break;}}if(stu.get(num).getSex()==false){s="男";}else{s="女";}System.out.print("学号: ");System.out.print(stu.get(num).getId());System.out.print(" 姓名: ");System.out.print(stu.get(num).getName());System.out.print(" 年龄: ");System.out.print(stu.get(num).getAge());System.out.print(" 性别: ");System.out.print(s);System.out.print(" 成绩: ");System.out.println(stu.get(num).getScore());}}public static void showStudent(ArrayList<Student>stu) {String sex;for(int i=0;i<stu.size();i++) {if(stu.get(i).getSex()==false){sex="男";}else{sex="女";}System.out.print("("+(i+1)+") ");System.out.print("学号: ");System.out.print(stu.get(i).getId());System.out.print(" 姓名: ");System.out.print(stu.get(i).getName());System.out.print(" 年龄: ");System.out.print(stu.get(i).getAge());System.out.print(" 性别: ");System.out.print(sex);System.out.print(" 成绩: ");System.out.println(stu.get(i).getScore());}}}
案例三
StudentDemo.Java
package Demo;import java.lang.*;
import java.util.*;public class StudentDemo{public static void main(String[] args) {int[] id;String[] name;String[] sex;String[] major;int[] age;int subscript;Scanner in = new Scanner(System.in);System.out.println("请输入你想要创建学生信息的人数:");subscript=in.nextInt();id=new int[subscript];name=new String[subscript];sex=new String[subscript];major=new String[subscript];age=new int[subscript];for(int q=0;q<1000;q++) {menu();int x=in.nextInt();switch(x) {case 1:add(id,name,age,sex,major);break;case 2:del(id,name,age,sex,major);break;case 3:search(id,name,age,sex,major);break;case 4:rewrite(id,name,age,sex,major);break;case 5:sort(id,name,age,sex,major);break;case 6:print(id,name,age,sex,major);break;case 0:System.exit(0);break;default:System.out.println("---输入错误 请重新输入!!!"); menu();break;}} }//管理员菜单public static void menu() {Scanner sc=new Scanner(System.in);System.out.println(" **************************************");System.out.println(" -- 管理员 你好 ");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("*** 6.显示学生信息 ***");System.out.println("*** 0.退出 ***");System.out.println("*****************************************");System.out.println("请输入你的选择");}//增加private static void add(int[] id,String[] name,int[] age,String[] sex,String[] major) {Scanner ss=new Scanner(System.in);for(int i=0;i<id.length;i++) {System.out.println("请输入学号:");id[i]=ss.nextInt();ss.nextLine();System.out.println("请输入名字:");name[i]=ss.nextLine();System.out.println("请输入年龄:");age[i]=ss.nextInt();ss.nextLine();System.out.println("请输入性别:");sex[i]=ss.nextLine();System.out.println("请输入专业:");major[i]=ss.nextLine(); System.out.println("*******************"); }System.out.println("添加成功!!"); }//删除private static void del(int[] id,String[] name,int[] age,String[] sex,String[] major) {System.out.println("请输入要删除的学号:");Scanner sp=new Scanner(System.in);int find=0;int num=sp.nextInt();for(int i=0;i<id.length;i++) {if(num==id[i]){id[i]=id[i+1]; id = Arrays.copyOf(id, id.length-1);name[i]=name[i+1]; name= Arrays.copyOf(name, name.length-1);age[i]=age[i+1]; age = Arrays.copyOf(age, age.length-1);sex[i]=sex[i+1]; sex = Arrays.copyOf(sex, sex.length-1);major[i]=major[i+1]; major = Arrays.copyOf(major, major.length-1);id[i]=0;name[i]=null;age[i]=0;sex[i]=null;major[i]=null;i--;find=1;System.out.println("删除成功!!");break;} } if(find==0) {System.out.println("暂无该学生信息存入!!");} }//查询private static void search(int[] id,String[] name,int[] age,String[] sex,String[] major) {Scanner tt=new Scanner(System.in);System.out.println("------------");System.out.println("----1.学号------");System.out.println("----2.名字------");System.out.println("------------");System.out.println("想要查询的方式:");int a=tt.nextInt();if(a==1) {int find=0; System.out.println("请输入要查询的学号:");int num=tt.nextInt();for(int i=0;i<id.length;i++) {if(num==id[i]) {System.out.println("学号:"+id[i]);System.out.println("姓名:"+name[i]);System.out.println("年龄:"+age[i]);System.out.println("性别:"+sex[i]);System.out.println("专业:"+major[i]);find=1;System.out.println("查询成功!!"); break;} } if(find==0) {System.out.println("无该学号的信息!!");} }else if(a==2) {int s=0;System.out.println("请输入要查询的名字:");tt.nextLine();String name1=tt.nextLine();for(int m=0;m<id.length;m++) {if(name1.equals(name[m])) {System.out.println("学号:"+id[m]);System.out.println("姓名:"+name[m]);System.out.println("年龄:"+age[m]);System.out.println("性别:"+sex[m]);System.out.println("专业:"+major[m]);s=1; System.out.println("查询成功!!"); break; } }if(s==0) {System.out.println("无该学号的信息!!");}} }//修改private static void rewrite(int[] id,String[] name,int[] age,String[] sex,String[] major) {int num,a,c;String b,d,e;int find=0;Scanner sw=new Scanner(System.in);System.out.println("请输入要修改学生的学号:");num=sw.nextInt(); for(int i=0;i<id.length;i++) {if(num==id[i]) {System.out.println("请重新输入该学生信息:");System.out.println("--------"); System.out.println("修改该学生学号为:");a=sw.nextInt();id[i]=a;sw.nextLine();System.out.println("修改该学生名字为:"); b=sw.nextLine();name[i]=b;System.out.println("修改该学生年龄为:");c=sw.nextInt();age[i]=c;sw.nextLine();System.out.println("修改该学生性别为:");d=sw.nextLine();sex[i]=d;System.out.println("修改该学生专业为:");e=sw.nextLine();major[i]=e;find=1;System.out.println("修改信息成功!!");break;} }if(find==0) {System.out.println("暂无该学生信息存入!!");}}//排序private static void sort(int[] id,String[] name,int[] age,String[] sex,String[] major){Scanner op=new Scanner(System.in);int i,j,temp,a,c;String b,d,e;System.out.println("-----------------");System.out.println("----1.升序-----");System.out.println("----2.降序----");System.out.println("-----------------");System.out.println("请选择排序的方式:");int q=op.nextInt();if(q==1) { for(i=0;i<id.length-1;i++) {for(j=0;j<id.length-1;j++) {if(id[j]>id[j+1]) {temp=id[j];id[j]=id[j+1];id[j+1]=temp;b=name[j];name[j]=name[j+1];name[j+1]=b;c=age[j];age[j]=age[j+1];age[j+1]=c;d=sex[j];sex[j]=sex[j+1];sex[j+1]=d;e=major[j];major[j]=major[j+1];major[j+1]=e; }}}System.out.println("排序成功!!");}else if(q==2) {for(i=0;i<id.length-1;i++) {for(j=0;j<id.length-1;j++) {if(id[j]<id[j+1]) {temp=id[j];id[j]=id[j+1];id[j+1]=temp;b=name[j];name[j]=name[j+1];name[j+1]=b;c=age[j];age[j]=age[j+1];age[j+1]=c;d=sex[j];sex[j]=sex[j+1];sex[j+1]=d;e=major[j];major[j]=major[j+1];major[j+1]=e; }} }System.out.println("排序成功!!");} }//显示private static void print(int[] id,String[] name,int[] age,String[] sex,String[] major){for(int i=0;i<id.length;i++) {System.out.println("-----------------");System.out.println("学号:"+id[i]);System.out.println("姓名:"+name[i]);System.out.println("年龄:"+age[i]);System.out.println("性别:"+sex[i]);System.out.println("专业:"+major[i]);System.out.println("-----------------");}}}
案例四
Student.Java
package student_demo;public class Student {String num,name,sex;float score;public Student(String num,String name,String sex,float score){this.num =num;this.name =name;this.sex =sex;this.score =score;}public void setNum(String num) {this.num =num;}public void setName(String name) {this.name =name;}public void setSex(String sex) {this.sex =sex;}public void setScore(float score) {this.score =score;}public String getNum() {return this.num ;}public String getName() {return this.name ;}public String getSex() {return this.sex ;}public float getScore() {return this.score ;}public String toString(){ //学生信息String str="学号="+this.num+"\t\t"+"姓名="+this.name+"\t\t"+"性别="+this.sex+"\t\t"+"成绩="+this.score; return str;}}
StudentManage.java
package student_demo;import java.util.Arrays;
import java.util.Scanner;public class StudentManage {static boolean bool=true;static Student[] students=new Student[100];static int count=0;//主函数public static void main(String[] args) {// TODO Auto-generated method stubScanner sc=new Scanner(System.in);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(" 6:输出学生信息 ");System.out.println(" 7:退出系统 ");System.out.println("*******************************");System.out.println("请输入你要进行的操作(1-7):");int n;n=sc.nextInt();switch(n) {case 1:System.out.println("--------------增加学生信息--------------");addstudent();break;case 2:System.out.println("--------------删除学生信息--------------");deletestudent();break;case 3:System.out.println("--------------查找学生信息--------------");searchstudent();break;case 4:System.out.println("--------------修改学生信息--------------");changestudent();break;case 5:System.out.println("------------对学生信息进行排序------------");sortstudent();break;case 6:System.out.println("--------------输出学生信息--------------");printstudent();break;case 7:System.out.println("-------------感谢使用本系统!-------------");System.exit(0);break;default:System.out.println("-----------输入有误,请重新输入!-----------");}}}//增public static void addstudent() {Scanner sc=new Scanner(System.in);if(count>students.length-1) { //判断是否满员System.out.println("本系统已满!");return;}if(count<students.length){ System.out.println("请输入学生学号:");String num=sc.next();for(int i=0;i<count;i++) {if(students[i].getNum().equals(num)) {System.out.println("学号重复!");}}System.out.println("请输入学生姓名:");String name=sc.next();System.out.println("请输入学生性别:");String sex=sc.next();System.out.println("请输入学生成绩:");float score=sc.nextFloat();Student student=new Student(num,name,sex,score);students[count]=student;//将信息存入数组count++;}System.out.println("增加成功!");}//删public static void deletestudent() {Scanner sc=new Scanner(System.in);boolean flag = false;int count=0;System.out.println("请输入要删除的学生学号:");String num=sc.next();for(Student student: students) {if(student==null) {System.out.println("该系统暂时没有学生信息!请先添加学生信息!");break;} if(student.getNum().equals(num)) {while(count<students.length-1) {students[count] = students[count+1];count++;}students[students.length-1] = null; System.out.println("删除成功!删除后学生信息如下:");printstudent();flag=true;return;}count++;}if(!flag) {System.out.println("该学生信息不存在!");return;} }//查public static void searchstudent() { Scanner sc=new Scanner(System.in);boolean flag = false;System.out.println("请输入要查找的学生学号:");String num=sc.next();for(Student student: students) {if(student==null) {System.out.println("该系统暂时没有学生信息!请先添加学生信息!");break;} if(student.getNum().equals(num)) { System.out.println("查询成功!");System.out.println("学号="+student.getNum()+"\t"+"姓名="+student.getName()+"\t"+"性别="+student.sex+"\t"+"成绩="+student.getScore()); flag=true;break;} } if(!flag) {System.out.println("该学生信息不存在!");return;} }//改public static void changestudent() {Scanner sc=new Scanner(System.in);System.out.println("请输入要修改的学生的学号:");String num=sc.next();for(Student student:students) {if(student==null) {System.out.println("该系统暂时没有学生信息!请先添加学生信息!");break;} if(student.getNum().equals(num)) {System.out.println("请输入修改后的学生学号:");student.setNum(sc.next());System.out.println("请输入修改后的学生姓名:");student.setName(sc.next());System.out.println("请输入修改后的学生性别:");student.setSex(sc.next());System.out.println("请输入修改后的学生成绩:");student.setScore(sc.nextFloat());System.out.println("修改成功!修改后信息如下:");printstudent();return; } }System.out.println("未找到该学生信息,无法修改!");return;}//排序(根据成绩)public static void sortstudent() {for(Student student:students) {if(student==null) {System.out.println("该系统暂时没有学生信息!请先添加学生信息!");break;} for(int i=0;i<count;i++){for(int j=i+1;j<count;j++)if(students[i].getScore()<students[j].getScore()) {Student temp=students[i];students[i]=students[j];students[j]=temp;}}System.out.println("排序完成!排序后信息如下:");printstudent();return;}}//输出public static void printstudent() { if(count!=0) {for(int i=0;i<count;i++) {System.out.println("学号="+students[i].getNum()+"\t"+"姓名="+students[i].getName()+"\t"+"性别="+students[i].sex+"\t"+"成绩="+students[i].getScore()); }}else {System.out.println("该系统暂时没有学生信息!请先添加学生信息!");}}}
案例五
Student.Java
public class Student {private int number;private String name;private int age;private int python;private int java;private int linux;private int sql;private int sum;private int avg;public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}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 int getPython() {return python;}public void setPython(int python) {this.python = python;}public int getJava() {return java;}public void setJava(int java) {this.java = java;}public int getLinux() {return linux;}public void setLinux(int linux) {this.linux = linux;}public int getSql() {return sql;}public void setSql(int sql) {this.sql = sql;}public int getSum() {return sum;}public void setSum() {this.sum = this.python+this.java+this.linux+this.sql;}public int getAvg() {return avg;}public void setAvg() {this.avg = this.sum/4;}public String toString(){String str="\t\t"+this.number+"\t\t"+this.name+"\t\t"+this.age+"\t\t"+this.python+"\t\t"+this.java+"\t\t"+this.linux+"\t\t"+this.sql+"\t\t"+this.sum+"\t\t"+this.avg;return str;}
}
StudentAdmin.java
public class StudentAdmin {static String msg = "\t\t编号\t\t姓名\t\t年龄\t\tpython\tjava\tlinux\tsql\t\t总分\t\t平均分";public void menu() {System.out.println("****************************************************");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("** 6:根据某门成绩进行排序 **");System.out.println("** 7:根据总分进行排序 **");System.out.println("** 0:退出系统 **");System.out.println("----------------------------------------------------");System.out.println("***************************************************");}public int index(Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] == null) {return i;}}return 999;}public void print(Student[] arr) {System.out.println(msg);for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {System.out.println(arr[i]);}}}public void create(String name, int age, Student[] arr) {int index = index(arr);if (index != 999) {Student stu = new Student();stu.setName(name);stu.setAge(age);stu.setNumber(index);arr[index] = stu;System.out.println("添加学生成功");this.print(arr);} else {System.out.println("班级学生已满,无法再添加学生了");}}public void select(int number, Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {if (arr[i].getNumber() == number) {System.out.println(msg);System.out.println(arr[i]);return;}}}System.out.println("没有这个编号的学生");}public void update(int number, String name, int age, Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {if (arr[i].getNumber() == number) {arr[i].setName(name);arr[i].setAge(age);System.out.println("更新学生信息成功");this.print(arr);return;}}}System.out.println("没有这个学生");}public void delete(int number, Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {if (arr[i].getNumber() == number) {arr[i] = null;System.out.println("删除成功");this.print(arr);return;}}}System.out.println("没有这个编号的学生");}public void input(int number, int python, int java, int linux, int sql, Student[] arr) {for (int i = 0; i < arr.length; i++) {if (arr[i] != null) {if (arr[i].getNumber() == number) {arr[number].setPython(python);arr[number].setJava(java);arr[number].setLinux(linux);arr[number].setSql(sql);arr[number].setSum();arr[number].setAvg();System.out.println("录入成绩成功");this.print(arr);return;}}}System.out.println("不存在这个学生");}//15:根据某门成绩排序public void courseSort(int number, Student[] arr) {switch (number) {case 1:for (int i = 0; i < arr.length - 1; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] != null && arr[j] != null) {if (arr[i].getPython() < arr[j].getPython()) {Student temp = arr[i];//比较某门成绩,交换学生arr[i] = arr[j];arr[j] = temp;}}}}break;case 2:for (int i = 0; i < arr.length - 1; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] != null && arr[j] != null) {if (arr[i].getJava() < arr[j].getJava()) {Student temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}break;case 3:for (int i = 0; i < arr.length - 1; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] != null && arr[j] != null) {if (arr[i].getLinux() < arr[j].getLinux()) {Student temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}break;case 4:for (int i = 0; i < arr.length - 1; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] != null && arr[j] != null) {if (arr[i].getSql() < arr[j].getSql()) {Student temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}break;}this.print(arr);}//16:根据总分排序public void sumSort(Student[] arr) {for (int i = 0; i < arr.length - 1; i++) {for (int j = i + 1; j < arr.length; j++) {if (arr[i] != null && arr[j] != null) {if (arr[i].getSum() < arr[j].getSum()) {Student temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}this.print(arr);}}
StudentTest.java
import java.util.Scanner;public class StudentTest {public static void main(String[] args) {Scanner input=new Scanner(System.in);String account;String password;do {System.out.println("请输入账号和密码,中间用空格间隔:");account=input.next();password=input.next();if(!(account.equals("0"))) {System.out.println("账号输入错误!请重新输入账号和密码");}else if(!(password.equals("0"))) {System.out.println("密码输入错误!请重新输入账号和密码");}}while(!(account.equals("0")&&password.equals("0")));System.out.println("成功登陆!!!");Scanner sc=new Scanner(System.in);System.out.println("请定义学生人数");int stuNum=sc.nextInt();Student[] stuArr=new Student[stuNum];StudentAdmin admin=new StudentAdmin();while(true){admin.menu();int number=sc.nextInt();if(number==1){System.out.println("请输入学生姓名");String name=sc.next();System.out.println("请输入学生年龄");int age=sc.nextInt();admin.create(name,age,stuArr);}else if(number==2){System.out.println("请输入你要查找的学生的编号");int num=sc.nextInt();admin.select(num,stuArr);}else if(number==3){System.out.println("请输入你要更新的学生的编号");int num=sc.nextInt();System.out.println("请输入学生姓名");String name=sc.next();System.out.println("请输入学生年龄");int age=sc.nextInt();admin.update(num,name,age,stuArr);}else if(number==4){System.out.println("请输入你要删除的学生编号");int num=sc.nextInt();admin.delete(num,stuArr);}else if(number==5){System.out.println("请输入学生编号");int num=sc.nextInt();System.out.println("请输入学生python成绩");int python=sc.nextInt();System.out.println("请输入学生java成绩");int java=sc.nextInt();System.out.println("请输入学生linux成绩");int linux=sc.nextInt();System.out.println("请输入学生sql成绩");int sql=sc.nextInt();admin.input(num,python,java,linux,sql,stuArr);}else if (number==6){System.out.println("请选择你要排序的课程(1:python 2:java 3:linux 4:sql)");int courseNum=sc.nextInt();admin.courseSort(courseNum,stuArr);}else if (number==7){admin.sumSort(stuArr);}else if(number==0){System.exit(0);}else{System.out.println("请重新输入");}}}}