java————数组 简单写出一个管理系统

2023-05-26,,

  

数组的特点

1,  数组是一块连续的空间,下标描述空间的位置。

2,  下标从0开始,最大下标为数组长度—1。(*.length-1)

3,  数组元素都是变量。(就是每个下标对应的内容)。变量的类型为定义数组时的类型。

4,  数组创建后,会对每个数组进行初始化。

Int ——>0

Double——>0.0

引用类型——>null

5,  数组创建后,长度不能改变。

只用数组简单写出一个学生管理系统

public class Student {
// 定义学号数组
static int[] xh = new int[20];
// 定义姓名数组
static String[] xm = new String[20];
// 定义成绩数组
static int[] cj = new int[20];
// 定义输入次数
static int count = 0;

//主入口
public static void main(String[] args) {
dl();
while (true) {
String itme = JOptionPane.showInputDialog(null, "1,添加\n2,显示\n3,删除\n4,查找\n5,修改\n6,排序\n7,退出");
switch (itme) {
case "1":
add();
break;
case "2":
xs();
break;
case "3":
delete();
break;
case "4":
seek();
break;
case "5":
amend();
break;
case "6":
jx();
break;
case "7":
System.exit(0);
}
}
}

// 登录
public static boolean dl() {
JOptionPane.showMessageDialog(null, "欢迎光临");
int index = -1;
for (int i = 0; i < 3; i++) {
String y = JOptionPane.showInputDialog(null, "请输入用户名");
String m = JOptionPane.showInputDialog(null, "请输入密码");
if (y.equals("java") && m.equals("123")) {
JOptionPane.showMessageDialog(null, "登陆成功");
break;

} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误,请从新输入");
index += i;

}
}
if (index != -1) {
JOptionPane.showMessageDialog(null, "非法用户");

}
return false;

}

//因为查找,修改,删除都有一个功能,那就是先找出一个名字,在功能于该名字
//那么就可以再单独写一个找这个名字的方法,后面的功能直接调用就行了

//找该名字的下标,返回int类型,返回值为-1;
public static int findMyName(){
if(count ==0){
JOptionPane.showMessageDialog(null, "没有学生信息");
return -1;
}else{
String name = JOptionPane.showInputDialog(null, "请输入学生的名字:");
for (int i = 0; i < count; i++) {
if (name.equals(xm[i])) {
return i; //找到你要找的学生所在的下标位置
}
}
JOptionPane.showMessageDialog(null, "没有找到此人");
return -1;//没找到就返回-1
}
}
// 添加
public static void add() {
int xh1 = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入学号:"));
xh[count] = xh1;
String name = JOptionPane.showInputDialog(null, "请输入姓名:");
xm[count] = name;
int cj1 = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入成绩:"));
cj[count] = cj1;
count++;
}

// 显示
public static void xs() {
if(count !=0){
String all = "学号 姓名 成绩\n";
for (int i = 0; i < count; i++) {
all += xh[i] + " " + xm[i] + " " + cj[i] + "\n";
}
JOptionPane.showMessageDialog(null, all);
}else
JOptionPane.showMessageDialog(null, "没有学生信息");
}

// 删除
public static void delete() {
int index =findMyName();
if (index != -1) {
for(int i=index;i<count-1;i++){
xh[i] = xh[i+1];
xm[i] = xm[i+1];
cj[i] = cj[i+1];
}
count--;
xs();
}
}

// 查找
public static void seek() {
int index = findMyName();
if (index != -1) {
String all = "学号 姓名 成绩\n";
all += xh[index] + " " + xm[index] + " " + cj[index] + "\n";
JOptionPane.showMessageDialog(null, all);
}
}

// 修改
public static void amend() {
int index = findMyName();
if (index != -1) {
xh[index] =Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学号:"));
xm[index] =JOptionPane.showInputDialog(null,"请输入姓名");
cj[index] =Integer.parseInt(JOptionPane.showInputDialog(null,"请输入成绩"));
xs();
}

}

// 降序输出
public static void jx() {
for (int i = 0; i < cj.length; i++) {
for (int j = i + 1; j < cj.length; j++) {
if (cj[i] < cj[j]) {
int gread = cj[j];
cj[j] = cj[i];
cj[i] = gread;
String name = xm[j];
xm[j] = xm[i];
xm[i] = name;
int number = xh[j];
xh[j] = xh[i];
xh[i] = number;
}
}
}
xs();
}
}

java————数组 简单写出一个管理系统的相关教程结束。

《java————数组 简单写出一个管理系统.doc》

下载本文的Word格式文档,以方便收藏与打印。