学生成绩管理系统小程序

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, "输入有误,请重新输入(1-7)");
            }    
        }
    }
    
    public static boolean login(){//登录
        for (int i=0; i<3; i++){
            String userName = JOptionPane.showInputDialog(null,"请输入用户名");
            int pwd = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入密码"));
            if (userName.equals("lwz") && pwd == 1992){
                return true;
            }
            else{
                JOptionPane.showMessageDialog(null,"用户名或密码错误");
            }
        }
        return false;
    }
    
    public static void addStudent(){//添加学生信息
        String codeStr = JOptionPane.showInputDialog(null,"请输入学号");
        String nameStr = JOptionPane.showInputDialog(null,"请输入姓名");
        double grade = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入成绩"));
        number[num] = codeStr;
        name[num] = nameStr;
        score[num] = grade;
        num++;
    }
    
    public static void showStudent(){//显示学生信息
        String str = "学号      姓名      成绩\n";
        for (int i=0; i<num; i++){
//            if (number[i] == null)
//                continue;
            str += number[i]+"        "+name[i]+"        "
                    +score[i]+"\n";    
        }
        JOptionPane.showMessageDialog(null,str);
    }
    
    public static void deleteStudent(){//删除学生信息
        String input = JOptionPane.showInputDialog(null, "请输入姓名");
        int index = -1;
        
        for (int i=0; i<num; i++){
            if (name[i].equals(input)){
                index = i;
            }
        }
        if (index == -1){
            JOptionPane.showMessageDialog(null, "没有找到该学生");
            return;
        }
        for (int i=index; i<num; 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 = "学号           姓名             成绩\n";
        String input = JOptionPane.showInputDialog(null,"请输入姓名");
        
        for (int i=0; i<num; i++){
            if (name[i].equals(input)){
                index = i;
                str += number[i]+"      "+name[i]+"     "+score[i]+"\n";
            }
        }
        if (index == -1){
            JOptionPane.showMessageDialog(null,"没有找到该学生");
        }
        else{
            JOptionPane.showMessageDialog(null,str);
        }
    }
    
    public static void updateStudent(){//修改
        int index = -1;
        String input = JOptionPane.showInputDialog(null,"请输入姓名");
        for (int i=0; i<num; i++){
            if (name[i].equals(input)){
                index = i;
                String strNum = JOptionPane.showInputDialog(null,"请输入学号");
                number[i] = strNum;
                String strName = JOptionPane.showInputDialog(null,"请输入姓名");
                name[i] = strName;
                double s = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入成绩"));
                score[i] = s;
            }
        }
        if (index == -1){
            JOptionPane.showMessageDialog(null, "没有找到该学生");
            return;
        }
        String str = "学号        姓名          成绩\n";
        for (int i=0; i<num; i++){
            str += number[i]+"        "+name[i]+"        "+score[i]+"\n";
        }
        JOptionPane.showMessageDialog(null, str);
    }

    public static void sortStudent(){//排序
        
        for (int i=0; i<num; i++){//选择排序
            for (int j=i+1; j<num; 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();
    }
}

posted @ 2017-03-08 21:43  沉迷学习,日渐消瘦  阅读(763)  评论(0)    收藏  举报