Java学习笔记day5--上一个练习题的优化(封装)

package day5_oop1;
/*
 练习题3:
 定义Student类,包含三个属性:学号number(int),年级state(int)。成绩score(int),创建20个学生对象,学号为1到20
 年级和成绩由随机数生成。
 问题一:打印出年级为3的学生信息
 问题二:使用冒泡排序法按学生成绩排序,并遍历所有学生信息
 提示:
 1.生成随机数 double Math.random()
 2.四舍五入取整:Math.round(double d),返回类型long
 */
//================================================================================================
//这是对上个版本的改进,将遍历学生信息、查找某个年级的学生、按成绩给学生排序进行了封装。
//================================================================================================
public class OopExam3Update {
    public static void main(String[] args) {
        //============================================================================
        //如何一次性定义20个对象?
        Students1[] stu=new Students1[20];//声明一个Students类型的数组
        for(int i=0;i<20;i++) {
            stu[i]=new Students1();//给每一个stu对象赋值*******************************
            stu[i].number=i+1;
            stu[i].state=(int)(Math.random()*6+1);
            stu[i].score=(int)(Math.random()*100+1);
        }
        OopExam3Update oop=new OopExam3Update();//要想调用方法,必须创建对象
        //1.遍历输出学生信息
        oop.print(stu);
        //2.打印所有三年级学生信息
        oop.stateSearch(stu, 3);
        //3.使用冒泡排序法对学生重新排序
        oop.scoreSort(stu);
        //4.打印重新排序后的学生信息
        oop.print(stu);
        /*问题一
        System.out.println("第一问");
        for(int i=0;i<20;i++) {
            if(stu[i].state==3) {
                stu[i].infoPrint();
                System.out.println();
            }
        }*/
        
        /*问题二
        System.out.println("第二问");
        for(int i=0;i<stu.length-1;i++) {
            for(int j=0;j<stu.length-1-i;j++) {
                if(stu[j].score>stu[j+1].score) {
                    //如果需要换序,交换的是数组的元素,是一个Students对象!!!!!
                    Students1 temp=new Students1();//注意注意,不仅仅只是对成绩进行排序,还要把其他信息和成绩作为一个整体
                    temp=stu[j];
                    stu[j]=stu[j+1];
                    stu[j+1]=temp;
                }
            }
        }
        for(int i=0;i<stu.length;i++) {
            stu[i].infoPrint();
            System.out.println();
        }*/
        
    }
    //1.遍历数组
    public void print(Students1[] stu) {
        for(int i=0;i<stu.length;i++) {
            System.out.println(stu[i].infoPrint());
        }
    }
    //2.查找某个年级的所有学生并遍历输出学生信息
    public void stateSearch(Students1[] stu,int grade) {
        for(int i=0;i<stu.length;i++) {
            if(stu[i].state==3) {
                System.out.println(stu[i].infoPrint());
            }
        }
    }
    //3.按成绩排序
    public void scoreSort(Students1[] stu) {
        for(int i=0;i<stu.length-1;i++) {
            for(int j=0;j<stu.length-1-i;j++) {
                if(stu[j].score>stu[j+1].score) {
                    Students1 temp=stu[j];
                    stu[j]=stu[j+1];
                    stu[j+1]=temp;
                }
            }
        }
    }
}
class Students1{
    int number;
    int state;
    int score;
    
    public String infoPrint() {
        return "学号:"+number+",年级:"+state+",成绩"+score;
    }
}

 

posted @ 2022-11-06 21:23  乐美  阅读(43)  评论(0)    收藏  举报