考试成绩查询

文档需求:* 定义学生类(多个科目成绩)* 输入多个学生成绩信息* 按照不同科目成绩进行排序输出

/**
 * 定义学生类(多个科目成绩)
 */
public class Student {
    String name;
    double chineseScore;
    double mathScore;
    double englishScore;

    public Student() {
    }

    public Student(String name, double chineseScore, double mathScore, double englishScore) {
        this.name = name;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }
}
/**
 * 学生成绩信息
 */
public class StudentScore {
    //创建学生成绩数组,长度为5
    Student[] studentScoreArrays = new Student[5];

    public void setScore(int index, Student name) {//添加成绩类到成绩数组中
        studentScoreArrays[index] = name;
    }

    public Student[] getScore() { //返回学生成绩
        return studentScoreArrays;
    }

}
/**
 * 按照不同科目成绩进行排序输出
 */
public class Task {
    public static void main(String[] args) {
        //获取用户输入
        Scanner scanner = new Scanner(System.in);
        //创建一个学生成绩对象
        StudentScore studentScore = new StudentScore();
        //输入多个学生成绩信息
        inputSeveralStudentScoreInfomation(scanner, studentScore);
        //打印学生成绩表
        Student[] print = printStudentScore(studentScore);
        //对学生成绩表排序,按照不同科目成绩进行排序输出
        System.out.println("是否进行排序? 是y 否n");
        String flag = scanner.next();
        if (flag.equals("y")) {
            while (true) {
                System.out.println("输入要排序的科目:1语文 2数学 3英语 9退出");
                int subject = scanner.nextInt();
                switch (subject) {
                    case 1:
                        //找到语文成绩,对语文成绩进行降序排序
                        sortChinese(print);
                        break;
                    case 2:
                        //找到数学成绩,对数学成绩进行降序排序
                        sortMath(print);
                        break;
                    case 3:
                        //找到英语成绩,对英语成绩进行降序排序
                        sortEnglish(print);
                        break;
                    case 9:
                        System.out.println("结束排序");
                        return;
                }
            }
        } else {
            System.out.println("-----程序结束-----");
            printStudentScore(studentScore);
            System.out.println("-----------------");

        }
    }

    /*
    打印学生成绩表
    */
    private static Student[] printStudentScore(StudentScore studentScore) {
        System.out.println("==============学生成绩表==============");
        System.out.println("姓名\t语文成绩\t\t数学成绩\t\t英语成绩");
        Student[] print = studentScore.getScore();
        //使用增强for循环
        for (Student stu : print) {
            System.out.println(stu.name + "\t\t" + stu.chineseScore + "\t\t" + stu.mathScore + "\t\t" + stu.englishScore);
        }
        return print;
    }

    /*
     输入多个学生成绩信息
    */
    private static void inputSeveralStudentScoreInfomation(Scanner scanner, StudentScore studentScore) {
        //定义一个长度为5的学生成绩表
        for (int i = 0; i < studentScore.studentScoreArrays.length; i++) {
            System.out.println("请输入姓名");
            String inputName = scanner.next();
            System.out.println("请输入语文成绩");
            double inputChinese = scanner.nextDouble();
            System.out.println("请输入数学成绩");
            double inputMath = scanner.nextDouble();
            System.out.println("请输入英语成绩");
            double inputEnglish = scanner.nextDouble();
            Student studentIn = new Student(inputName, inputChinese, inputMath, inputEnglish);
            //把成绩传给学生成绩数组
            studentScore.setScore(i, studentIn);
        }
    }

    /*
    找到英语成绩,对英语成绩进行降序排序
     */
    private static void sortEnglish(Student[] print) {
        //冒泡排序(降序)
        for (int i = 0; i < print.length - 1; i++) {
            for (int j = 0; j < print.length - 1 - i; j++) {
                String tempName;
                double tempChineseScore;
                double tempMathScore;
                double tempEnglishScore;
                if (print[j].englishScore < print[j + 1].englishScore) {
                    //名字对应英语成绩排序
                    tempName = print[j].name;
                    print[j].name = print[j + 1].name;
                    print[j + 1].name = tempName;
                    //语文对应名字成绩排序
                    tempChineseScore = print[j].chineseScore;
                    print[j].chineseScore = print[j + 1].chineseScore;
                    print[j + 1].chineseScore = tempChineseScore;
                    //数学对应名字成绩排序
                    tempMathScore = print[j].mathScore;
                    print[j].mathScore = print[j + 1].mathScore;
                    print[j + 1].mathScore = tempMathScore;
                    //英语成绩(降序)
                    tempEnglishScore = print[j].englishScore;
                    print[j].englishScore = print[j + 1].englishScore;
                    print[j + 1].englishScore = tempEnglishScore;
                }
            }
        }
        System.out.println("------------------------------------");
        System.out.println("对英语成绩降序排序后");
        System.out.println("==============学生成绩表==============");
        System.out.println("姓名\t语文成绩\t\t数学成绩\t\t英语成绩");
        //使用增强for循环
        for (Student stu : print) {
            System.out.println(stu.name + "\t\t" + stu.chineseScore + "\t\t" + stu.mathScore + "\t\t" + stu.englishScore);
        }
    }

    /*
    找到数学成绩,对数学成绩进行降序排序
     */
    private static void sortMath(Student[] print) {
        //冒泡排序(降序)
        for (int i = 0; i < print.length - 1; i++) {
            for (int j = 0; j < print.length - 1 - i; j++) {
                String tempName;
                double tempChineseScore;
                double tempMathScore;
                double tempEnglishScore;
                //判断条件是数学成绩(降序)
                if (print[j].mathScore < print[j + 1].mathScore) {
                    //名字对应数学成绩排序
                    tempName = print[j].name;
                    print[j].name = print[j + 1].name;
                    print[j + 1].name = tempName;
                    //语文对应名字成绩排序
                    tempChineseScore = print[j].chineseScore;
                    print[j].chineseScore = print[j + 1].chineseScore;
                    print[j + 1].chineseScore = tempChineseScore;
                    //数学成绩(降序)
                    tempMathScore = print[j].mathScore;
                    print[j].mathScore = print[j + 1].mathScore;
                    print[j + 1].mathScore = tempMathScore;
                    //英语对应名字成绩排序
                    tempEnglishScore = print[j].englishScore;
                    print[j].englishScore = print[j + 1].englishScore;
                    print[j + 1].englishScore = tempEnglishScore;
                }
            }
        }
        System.out.println("------------------------------------");
        System.out.println("对数学成绩降序排序后");
        System.out.println("==============学生成绩表==============");
        System.out.println("姓名\t语文成绩\t\t数学成绩\t\t英语成绩");
        //使用增强for循环
        for (Student stu : print) {
            System.out.println(stu.name + "\t\t" + stu.chineseScore + "\t\t" + stu.mathScore + "\t\t" + stu.englishScore);
        }
    }

    /*
   找到语文成绩,对语文成绩进行降序排序
    */
    private static void sortChinese(Student[] print) {
        //冒泡排序(降序)
        for (int i = 0; i < print.length - 1; i++) {
            for (int j = 0; j < print.length - 1 - i; j++) {
                String tempName;
                double tempChineseScore;
                double tempMathScore;
                double tempEnglishScore;
                //判断条件是语文成绩(降序)
                if (print[j].chineseScore < print[j + 1].chineseScore) {
                    //名字对应语文成绩排序
                    tempName = print[j].name;
                    print[j].name = print[j + 1].name;
                    print[j + 1].name = tempName;
                    //语文成绩(降序)
                    tempChineseScore = print[j].chineseScore;
                    print[j].chineseScore = print[j + 1].chineseScore;
                    print[j + 1].chineseScore = tempChineseScore;
                    //数学对应名字成绩排序
                    tempMathScore = print[j].mathScore;
                    print[j].mathScore = print[j + 1].mathScore;
                    print[j + 1].mathScore = tempMathScore;
                    //英语对应名字成绩排序
                    tempEnglishScore = print[j].englishScore;
                    print[j].englishScore = print[j + 1].englishScore;
                    print[j + 1].englishScore = tempEnglishScore;
                }
            }
        }
        System.out.println("------------------------------------");
        System.out.println("对语文成绩降序排序后");
        System.out.println("==============学生成绩表==============");
        System.out.println("姓名\t语文成绩\t\t数学成绩\t\t英语成绩");
        for (Student stu : print) {
            System.out.println(stu.name + "\t\t" + stu.chineseScore + "\t\t" + stu.mathScore + "\t\t" + stu.englishScore);
        }
    }
}

大致思路:在这里插入图片描述
编译器运行运行结果如下
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐