学习Java已经两周,对于文科生零基础的我来说,压力真的挺大的,不过还是得坚持啊,既然选择了,就要努力。上周五我们进行了一次考试,考的不是很理想,勉强及格,但对于Java的学习,及格是远远不够的,所以得付出更多的努力了,加油吧!
下面还是跟大家分享一个关于学生登录系统的项目:
import javax.swing.JOptionPane;
public class Xuesheng {
public static int[] cade = new int[20];
public static String[] name = new String[20];
public static int[] grade = new int[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 a = Integer.parseInt(s);
switch (a) {
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);
default:
JOptionPane.showInputDialog(null, "请输入选项");
}
}
}
public static void add() {
String x = JOptionPane.showInputDialog(null, "请输入学号");
String y = JOptionPane.showInputDialog(null, "请输入姓名");
String z = JOptionPane.showInputDialog(null, "请输入成绩");
cade[number] = Integer.parseInt(x);
name[number] = y;
grade[number] = Integer.parseInt(z);
number++;
// show();
}
public static void show() {
String s = "学号 姓名 成绩\n";
for (int i = 0; i < number; i++) {
s += cade[i] + " " + name[i] + " " + grade[i] + "\n";
}
JOptionPane.showMessageDialog(null, s);
}
public static void del() {
int index = findbyname();
if (index != -1) {
for (int i = index; i < number - 1; i++) {
cade[i] = cade[i + 1];
name[i] = name[i + 1];
grade[i] = grade[i + 1];
}
number--;
show();
}
}
public static void find() {
int index = findbyname();
if(index!=-1){
JOptionPane.showMessageDialog(null, "学号:" + cade[index] +" "+ " 姓名: "
+ name[index] +" "+ " 成绩: " + grade[index]);
}
}
public static void update() {
int index = findbyname();
String p = JOptionPane.showInputDialog(null, "请重新输入学号");
String f = JOptionPane.showInputDialog(null, "请重新输入姓名");
String t = JOptionPane.showInputDialog(null, "请重新输入成绩");
cade[index] = Integer.parseInt(p);
name[index] = f;
grade[index] = Integer.parseInt(t);
show();
}
public static void sort() {
int temp;
for(int i=0;i<number;i++){
for(int j = i+1;j<number;j++){
if(grade[i]<grade[j]){
temp=grade[i];
grade[i]=grade[j];
grade[j]=temp;
temp=cade[i];
cade[i]=cade[j];
cade[j]=temp;
String tp=name[i];
name[i]=name[j];
name[j]=tp;
}
}
}
show();
}
public static int findbyname() {
String m = JOptionPane.showInputDialog(null, "请输入姓名");
int index = -1;
for (int i = 0; i < number; i++) {
if (m.equals(name[i])) {
index = i;
return i;
}
}
if (index == -1) {
JOptionPane.showMessageDialog(null, "查无此人");
}
return -1;
}
public static boolean login() {
for (int i = 0; i < 3; i++) {
String name = JOptionPane.showInputDialog(null, "请输入用户名");
String pwd = JOptionPane.showInputDialog(null, "请输入密码");
if (name.equals("陈建勇") && pwd.equals("123456")) {
return true;
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
return false;
}
}