首页 > 代码库 > 学习第三周
学习第三周
学生管理系统
----------------------------------------------------------------------------------------------------------------------------
public class StudentManager {
/**学生数组*/
public static Student[] stuArray = new Student[20];
/**学生人数*/
public static int number = 0;
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临");
boolean isLand = login();
if(isLand == false){
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while(true){
String s = JOptionPane.showInputDialog(null,
"1、添加\n2、显示\n3、删除\n4、查找\n5、修改\n6、排序\n7、退出");
int item = Integer.parseInt(s);
switch(item){
case 1:
add();
break;
case 2:
show();
break;
case 3:
del();
break;
case 4:
find();
break;
case 5:
update();
break;
case 6:
sort();
break;
case 7:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "请选择1-7");
break;
}
}
}
/**
* 排序
*/
public static void sort(){
for(int i=0;i<number;i++){
for(int j=i+1;j<number;j++){
if(stuArray[i].grade < stuArray[j].grade){
Student s = stuArray[i];
stuArray[i] = stuArray[j];
stuArray[j] = s;
}
}
}
show();
}
/**
* 修改
*/
public static void update(){
int index = findByName();
if(index != -1){
String codeStr = JOptionPane.showInputDialog(null,"请重新输入学号");
String nameStr = JOptionPane.showInputDialog(null,"请重新输入姓名");
String gradeStr = JOptionPane.showInputDialog(null,"请重新输入成绩");
stuArray[index].code = Integer.parseInt(codeStr);
stuArray[index].name = nameStr;
stuArray[index].grade = Integer.parseInt(gradeStr);
show();
}
}
/**
* 查找
*/
public static void find(){
int index = findByName();
if(index != -1){
JOptionPane.showMessageDialog(null,
"学号:"+stuArray[index].code+" 姓名:"+stuArray[index].name+" 成绩:"+stuArray[index].grade);
}
}
/**
* 删除
*/
public static void del(){
int index = findByName();
if(index != -1){
for(int i = index;i<number;i++){
stuArray[i] = stuArray[i+1];
}
number--;
show();
}
}
/**
* 显示
*/
public static void show(){
String str = "学号 姓名 成绩\n";
for(int i=0;i<number;i++){
str += stuArray[i].code+" "+stuArray[i].name+" "+stuArray[i].grade+"\n";
}
JOptionPane.showMessageDialog(null, str);
}
/**
* 添加
*/
public static void add(){
String codeStr = JOptionPane.showInputDialog(null,"请输入学号");
String nameStr = JOptionPane.showInputDialog(null,"请输入姓名");
String gradeStr = JOptionPane.showInputDialog(null,"请输入成绩");
Student s = new Student();
s.code = Integer.parseInt(codeStr);
s.name = nameStr;
s.grade = Integer.parseInt(gradeStr);
stuArray[number] = s;
number ++;
show();
}
/**
* 登陆
* @return 登陆是否成功
*/
public static boolean login(){
for(int i=0;i<3;i++){
String userName = JOptionPane.showInputDialog(null,"请输入用户名");
String pwd = JOptionPane.showInputDialog(null,"请输入密码");
if(userName.equals("java") && pwd.equals("123")){
return true;
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
return false;
}
/**
* 根据姓名,查找该姓名所在的下标
* @return 姓名对应的下标,如果没有找到,返回-1
*/
public static int findByName(){
String name = JOptionPane.showInputDialog(null,"请输入姓名");
for(int i=0;i<number;i++){
if(name.equals(stuArray[i].name)){
return i;
}
}
JOptionPane.showMessageDialog(null, "查无此人");
return -1;
}
}
这里最大的问题是如何实现无限制的添加学生信息,但是目前无法做出来需要学习到以后的知识才能实现随后我们还学习了新的知识然后有了学生管理系统的改良版。
------------------------------------------------------------------------------------------------------------------------------------------------
package zhang;
import javax.swing.JOptionPane;
public class Student {
// 数组长度
// public static int number[] = new int[20];
// public static String name[] = new String[20];
// public static int score[] = new int[20];
public static int count = 0;
public static Student1[] stuarry=new Student1[20];
/**
* 判断是否登录成功
*
* @return是否登录
*/
public static boolean login() {
int a = 3;
String c = "12";
String d = "zh";
boolean island = false;
while (a >= 1) {
a--;
String code = JOptionPane.showInputDialog(null, "请输入帐号:");
String pwd = JOptionPane.showInputDialog(null, "请输入密码:");
if (pwd.equals(c) && code.equals(d)) {
island = true;
break;
} else {
JOptionPane.showMessageDialog(null, "密码错误");
}
}
return island;
}
/**
* 添加
*/
public static void add() {
Student1 gg=new Student1();
String a = JOptionPane.showInputDialog(null, "请输入学号");
gg.number= Integer.parseInt(a);
gg.name= JOptionPane.showInputDialog(null, "姓名");
String b = JOptionPane.showInputDialog(null, "请输入分数");
gg.score= Integer.parseInt(b);
stuarry[count]=gg;
count++;
}
/**
* 学生信息显示
*/
public static void showStudent() {
String str = " ";
for (int i = 0; i < count; i++) {
str += stuarry[i].number + " " + stuarry[i].name + " " + stuarry[i].score + "\n";
}
JOptionPane.showMessageDialog(null, str);
}
/**
* 删除
*/
public static void delete() {
String a = JOptionPane.showInputDialog(null, "请输入想要删除的学号");
int numbera = Integer.parseInt(a);
for (int i = 0; i < count; i++) {
if (stuarry[i].number == numbera) {
for (int j = i; j < count; j++) {
stuarry[j] = stuarry[j + 1];
}
count--;
}
}
}
/**
* 修改
*/
public static void change() {
int aa=find();
if(aa!=(-1)){
String b = JOptionPane.showInputDialog(null, "修改后的学号");
int numberb = Integer.parseInt(b);
String numberc = JOptionPane.showInputDialog(null, "修改后的姓名");
String d = JOptionPane.showInputDialog(null, "修改后的成绩");
int numberd = Integer.parseInt(d);
stuarry[aa].number = numberb;
stuarry[aa].name = numberc;
stuarry[aa].score = numberd;
}
}
/**
* 查询
*/
public static void select() {
int aa=find();
if (aa!=(-1)) {
JOptionPane.showMessageDialog(null, stuarry[aa]);
}
}
/**
* 排序
*/
public static void arrange() {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (stuarry[j].number < stuarry[j + 1].number) {
Student1 temp1= stuarry[j];
stuarry[j] = stuarry[j + 1];
stuarry[j + 1] = stuarry[i];
}
}
}
}
/**
* 按名字找下标
* @return想要找的名字的下标
*/
public static int find() {
String namea = JOptionPane.showInputDialog(null, "请输入想要查询 的学生名字");
for (int i = 0; i < count; i++) {
if ((stuarry[i].name).equals( namea)) {
return i;
}
}
JOptionPane.showMessageDialog(null, "查无此人");
return -1;
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎来到学生管理系统");
boolean island = login();
while (true) {
String a = JOptionPane.showInputDialog(null,
"1学生信息的添加add\n2显示show\n3删除delete\n4修改\n5查询select\n6排序\n7退出功能。");
int xuan = Integer.parseInt(a);
switch (xuan) {
case 1:
add();
break;
case 2:
showStudent();
break;
case 3:
delete();
break;
case 4:
change();
break;
case 5:
select();
break;
case 6:
arrange();
break;
case 7:
System.exit(0);
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------
这里需要记的新知识:
String s=JOptionPane.showInputDialog(null, "请输入帐号:");
//弹出输入框返回用户在对话框输入的内容
JOptionPane.showMessageDialog(null, str);
//弹出消息对话框,显示文字
int n=Integer.ParseInt(s);
//将字符串转化为整形
double a=Double.ParseDouble(s);
//将字符串类型转化为double类型
学习第三周