packageHw0420;importjavax.swing.JOptionPane;public classStuInfo {public static Student[] array = new Student[20];public static int stunum = 0;public static voidmain(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎使用XXX学生管理系统");boolean bl =login();if (!bl) {
JOptionPane.showMessageDialog(null, "非法用户");
}while (true) {
String s= JOptionPane.showInputDialog(null, "1.添加\n2.显示\n3.删除\n4.查找\n5.修改\n6.排序\n7.退出");int input =Integer.parseInt(s);switch(input) {case 1:
add();break;case 2:
show();break;case 3:
del();break;case 4:
find();break;case 5:
mod();break;case 6:
sort();break;case 7:
JOptionPane.showMessageDialog(null, "感谢使用XXX学生管理系统");
System.exit(0);break;
}
}
}/*** 登录判断
*
* @return是否登录成功*/
public static booleanlogin() {for (int i = 0; i < 3; i++) {
String id= JOptionPane.showInputDialog(null, "请输入您的账号");
String pwd= JOptionPane.showInputDialog(null, "请输入您的密码");if(id.equals(pwd)) {return true;
}
}return false;
}/*** 添加*/
public static voidadd() {
String strcode= JOptionPane.showInputDialog(null, "请输入学生学号");
String strname= JOptionPane.showInputDialog(null, "请输入学生姓名");
String strgrade= JOptionPane.showInputDialog(null, "请输入学生成绩");
Student s= newStudent();在此完成对学生对象各个属性的初始化
s.number=Integer.parseInt(strcode);
s.name=strname;
s.grade=Integer.parseInt(strgrade);
array[stunum]=s;
stunum++;
}/*** 查找学生
*
* @return找到返回下标 找不到返回-1*/
public static intfindByname() {
String s= JOptionPane.showInputDialog(null, "请输入您要查找的学生姓名");for (int i = 0; i < stunum; i++) {if(s.equals(array[i].name)) {returni;
}
}
JOptionPane.showMessageDialog(null, "查无此人");return -1;
}/*** 显示*/
public static voidshow() {
String str= "学号 姓名 成绩\n";for (int i = 0; i < stunum; i++) {
str+= array[i].number + " " + array[i].name + " " +array[i].grade+ "\n";
}
JOptionPane.showMessageDialog(null, str);
}/*** 删除*/
public static voiddel() {int index =findByname();if (index != -1) {for (int i = index; i < stunum; i++) {
array[i]= array[i + 1];
}
JOptionPane.showMessageDialog(null, "已删除");
show();
stunum--;
}
}/*** 查找*/
public static voidfind() {int index =findByname();if (index != -1) {
String str= "学号:" + array[index].number + "\n" + "姓名:" + array[index].name + "\n" + "成绩:"
+array[index].grade;
JOptionPane.showMessageDialog(null, str);
}
}/*** 修改*/
public static voidmod() {int index =findByname();if (index != -1) {
String strcode= JOptionPane.showInputDialog(null, "请输入学生学号");
String strname= JOptionPane.showInputDialog(null, "请输入学生姓名");
String strgrade= JOptionPane.showInputDialog(null, "请输入学生成绩");
array[index].number=Integer.parseInt(strcode);
array[index].name=strname;
array[index].grade=Integer.parseInt(strgrade);
}
show();
}/*** 排序*/
public static voidsort() {for (int i = 0; i < stunum; i++) {for (int j = i + 1; j < stunum; j++) {if (array[i].grade >array[j].grade) {
Student s=array[i];
array[i]=array[j];
array[j]=s;
}
}
}
show();
}
}