package day1; import javax.swing.JOptionPane; public class day1HomeWork { public static String[] number = new String[20]; public static String[] name = new String[20]; public static double[] score = new double[20]; public static int num = 0; public static void main(String[] args) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null,'歡迎光臨'); boolean flag = login(); if (flag == false){ JOptionPane.showMessageDialog(null,'非法用戶'); System.exit(-1); } while (true){ String input = JOptionPane.showInputDialog(null,'1、添加\n2、顯示\n' + '3、刪除\n4、查找\n5、修改\n6、排序\n7、退出'); char ch = input.toCharArray()[0]; switch (ch){ case '1': addStudent(); break; case '2': showStudent(); break; case '3': deleteStudent(); break; case '4': queryStudent(); break; case '5': updateStudent(); break; case '6': sortStudent(); break; case '7': JOptionPane.showMessageDialog(null,'謝謝使用'); System.exit(-1); break; default: JOptionPane.showMessageDialog(null, '輸入有誤,請(qǐng)重新輸入(1-7)'); } } } public static boolean login(){//登錄 for (int i=0; i<3; i++){ String userName = JOptionPane.showInputDialog(null,'請(qǐng)輸入用戶名'); int pwd = Integer.parseInt(JOptionPane.showInputDialog(null,'請(qǐng)輸入密碼')); if (userName.equals('lwz') && pwd == 1992){ return true; } else{ JOptionPane.showMessageDialog(null,'用戶名或密碼錯(cuò)誤'); } } return false; } public static void addStudent(){//添加學(xué)生信息 String codeStr = JOptionPane.showInputDialog(null,'請(qǐng)輸入學(xué)號(hào)'); String nameStr = JOptionPane.showInputDialog(null,'請(qǐng)輸入姓名'); double grade = Double.parseDouble(JOptionPane.showInputDialog(null,'請(qǐng)輸入成績(jī)')); number[num] = codeStr; name[num] = nameStr; score[num] = grade; num++; } public static void showStudent(){//顯示學(xué)生信息 String str = '學(xué)號(hào) 姓名 成績(jī)\n'; for (int i=0; i // if (number[i] == null) // continue; str += number[i]+' '+name[i]+' ' +score[i]+'\n'; } JOptionPane.showMessageDialog(null,str); } public static void deleteStudent(){//刪除學(xué)生信息 String input = JOptionPane.showInputDialog(null, '請(qǐng)輸入姓名'); int index = -1; for (int i=0; i if (name[i].equals(input)){ index = i; } } if (index == -1){ JOptionPane.showMessageDialog(null, '沒(méi)有找到該學(xué)生'); return; } for (int i=index; i number[i] = number[i+1]; name[i] = name[i+1]; score[i] = score[i+1]; } num--; showStudent(); } public static void queryStudent(){//查找 int index = -1; String str = '學(xué)號(hào) 姓名 成績(jī)\n'; String input = JOptionPane.showInputDialog(null,'請(qǐng)輸入姓名'); for (int i=0; i if (name[i].equals(input)){ index = i; str += number[i]+' '+name[i]+' '+score[i]+'\n'; } } if (index == -1){ JOptionPane.showMessageDialog(null,'沒(méi)有找到該學(xué)生'); } else{ JOptionPane.showMessageDialog(null,str); } } public static void updateStudent(){//修改 int index = -1; String input = JOptionPane.showInputDialog(null,'請(qǐng)輸入姓名'); for (int i=0; i if (name[i].equals(input)){ index = i; String strNum = JOptionPane.showInputDialog(null,'請(qǐng)輸入學(xué)號(hào)'); number[i] = strNum; String strName = JOptionPane.showInputDialog(null,'請(qǐng)輸入姓名'); name[i] = strName; double s = Double.parseDouble(JOptionPane.showInputDialog(null,'請(qǐng)輸入成績(jī)')); score[i] = s; } } if (index == -1){ JOptionPane.showMessageDialog(null, '沒(méi)有找到該學(xué)生'); return; } String str = '學(xué)號(hào) 姓名 成績(jī)\n'; for (int i=0; i str += number[i]+' '+name[i]+' '+score[i]+'\n'; } JOptionPane.showMessageDialog(null, str); } public static void sortStudent(){//排序 for (int i=0; i for (int j=i+1; j if (score[i] < score[j]){ String s = number[i]; number[i] = number[j]; number[j] = s; String n = name[i]; name[i] = name[j]; name[j] = n; double temp = score[i]; score[i] = score[j]; score[j] = temp; } } } showStudent(); } } |
|
來(lái)自: 文化龍鄉(xiāng) > 《教育之光》