用java数组实现录入学生成绩查看各科排序

考试成绩查询

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

/**
 * 定义学生类(多个科目成绩)
 */
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);
        }
    }
}

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

  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是对您的问题的回答: 1. 用java写借助一维数组实现Fibonacci数列前20项的显示程序 ```java public class Fibonacci { public static void main(String[] args) { int n = 20; int[] fib = new int[n]; fib[0] = 0; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } for (int i = 0; i < n; i++) { System.out.print(fib[i] + " "); } } } ``` 2. 用java写将数组中的全部数据逆向存放排列并输出的程序 ```java public class ReverseArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int n = arr.length; for (int i = 0; i < n/2; i++) { int temp = arr[i]; arr[i] = arr[n-i-1]; arr[n-i-1] = temp; } for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } } ``` 3. 用java写在数组中查找x是否存在,如果存在提示并输出,如果不存在做出提示的程序 ```java import java.util.Scanner; public class SearchArray { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; Scanner input = new Scanner(System.in); System.out.print("请输入要查找的数:"); int x = input.nextInt(); boolean flag = false; for (int i = 0; i < arr.length; i++) { if (arr[i] == x) { System.out.println("要查找的数在数组中的位置为:" + i); flag = true; break; } } if (!flag) { System.out.println("要查找的数不存在!"); } } } ``` 4. 用java编写一个程序,生成0-9之间的100个随机整数并且统计每一个数的产生的次数 ```java public class RandomNumbers { public static void main(String[] args) { int[] count = new int[10]; for (int i = 0; i < 100; i++) { int num = (int)(Math.random() * 10); count[num]++; } for (int i = 0; i < 10; i++) { System.out.println(i + "出现的次数为:" + count[i]); } } } ``` 5. 用java编写借助任意一种排序算法对某班的Java成绩进行升序排序。(冒泡或选择) ```java import java.util.Arrays; public class SortJavaScores { public static void main(String[] args) { int[] scores = {80, 90, 75, 60, 85}; for (int i = 0; i < scores.length-1; i++) { for (int j = 0; j < scores.length-1-i; j++) { if (scores[j] > scores[j+1]) { int temp = scores[j]; scores[j] = scores[j+1]; scores[j+1] = temp; } } } System.out.println(Arrays.toString(scores)); } } ``` 6. 用java编写假设一个组成绩给出,求其平均值,并获得平均值所在位置 ```java public class AverageScore { public static void main(String[] args) { int[] scores = {80, 90, 75, 60, 85}; int sum = 0; for (int i = 0; i < scores.length; i++) { sum += scores[i]; } double average = sum * 1.0 / scores.length; System.out.println("平均分为:" + average); // 获取平均分所在位置 for (int i = 0; i < scores.length; i++) { if (scores[i] == average) { System.out.println("平均分所在位置为:" + i); break; } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桥南小船sys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值