对象数组题目:使用冒泡排序按学生成绩排序

  • Post author:
  • Post category:其他



对象数组题目: 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。 问题一:打印出3年级(state值为3)的学生信息。 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息 提示: 1) 生成随机数:Math.random(),返回值类型double;   2) 四舍五入取整:Math.round(double d),返回值类型long。

package Test;

/**
 * @author 宇文智
 * @create 2020-07-01 10:33
 */
public class Expr4 {
    public static void main(String[] args) {
        Student[] students = inputStudentInfo(20);

        //遍历所有学生信息
        System.out.println("#########原始的学生信息#########");
        printStudentInfo(students);

        //使用冒泡排序按学生成绩排序
        System.out.println("#########按照成绩排序好的学生信息#########");
        sortStudentInfoByScore(students);
        printStudentInfo(students);

        //打印出3年级(state值为3)的学生信息
        System.out.println("##########3年级(state值为3)的学生信息########");
        findStudentInfoByState(students, 3);

    }

    private static void printStudentInfo(Student[] students) {
        for (Student stu : students) {
            System.out.println(stu.toString());
        }
    }

    private static void findStudentInfoByState(Student[] students, int state) {
        //二分查找,state是有序的。
//        int head = 0;
//        int end = students.length;
//        while (head < end) {
//            int middle = (head + end) / 2;
//            if (students[middle].state == state) {
//                System.out.println(state + "年级" + students[middle].toString());
//
//            } else if (students[middle].state > state) {
//                end = middle - 1;
//            } else {
//                head = middle + 1;
//            }
//        }
        for (int i = 0; i < students.length; i++) {
            if (students[i].state == state) {
                System.out.println(state + "年级" + students[i].toString());
            }
        }
    }

    private static void sortStudentInfoByScore(Student[] students) {
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - i - 1; j++) {
                if (students[j].score > students[j + 1].score) {
                    Student student = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = student;
                }
            }
        }
    }

    private static long getRandomNumber(int round, int start) {
        return (long) (Math.random() * round + start);
    }

    private static Student[] inputStudentInfo(int len) {
        Student[] students = new Student[len];
        for (int i = 0; i < len; i++) {
            students[i] = new Student(i + 1,
                    getRandomNumber(6, 1),
                    getRandomNumber(101, 0));
        }
        return students;
    }
}

class Student {
    int number;
    long state;
    long score;

    public Student(int number, long state, long score) {
        this.number = number;
        this.state = state;
        this.score = score;
    }

    public String toString() {
        return "学生信息:学号 " + number + " 年级 " + state + " 成绩 " + score;
    }
}



版权声明:本文为m0_38109926原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。