1022作业:冒泡排序-对象数组

一.作业要求

在这里插入图片描述




二.实现效果

1.创建3个类


在这里插入图片描述



2.完成学生类

public class Student {
    /**
     * 学生类
     */
    String name;//姓名
    int age;//年龄
    double score;//成绩
}



3.完成教室类

创建学生对象数组

Student add_student=new Student();

初始化学生方法

  • 由于此类有些复杂,我拆分成了两个
  • 一个用于添加学生,另一个就是主类

添加学生方法
public void verdict() {
        for (int i=0;i<stulist.length;i++){
            Student add_student=new Student();
            if(stulist[i]==null){
                System.out.print("请输入学生年龄:");
                add_student.age=ipt.nextInt();
                System.out.print("请输入学生姓名:");
                add_student.name=ipt.next();
                System.out.print("请输入学生成绩:");
                add_student.score=ipt.nextDouble();
                stulist[i]=add_student;
                break;
            }
        }

    }
主类
public void stuLnst(){
      for (int i=0;i<stulist.length;i++){
         Student add_student=new Student();
         switch (i){
             case 0:
                 System.out.println("请输入学生"+(i+1)+"的资料");
                 verdict();//添加学生
                 System.out.println(stulist[i].name);
              break;
             case 1:
                 System.out.println("请输入学生"+(i+1)+"的资料");
                 verdict();              		                         
                 System.out.println(stulist[i].name);
                 break;
             case 2:
                 System.out.println("请输入学生"+(i+1)+"的资料");
                 verdict();
                 System.out.println(stulist[i].name);
                 break;
             case 3:
                 System.out.println("请输入学生"+(i+1)+"的资料");
                 verdict();
                 System.out.println(stulist[i].name);
                 break;
             case 4:
                 System.out.println("请输入学生"+(i+1)+"的资料");
                 verdict();
                 System.out.println(stulist[i].name);
                 break;
             default:
                 break;
         }
     }

 }

排序-按成绩排序

public void Sorting_result(){
        double temp = 0;
        for (int i = 0; i < stulist.length - 1; i++){//轮数
            for (int j = 0; j < stulist.length - i - 1; j++) {   //每轮的次数a.Length - i - 1
                if (stulist[i+1]==null){
                    continue;
                }
                if ( stulist[j].score < stulist[j+1].score){
                    temp = stulist[j].score;
                    stulist[j].score = stulist[j+1].score;
                    stulist[j+1].score = temp;
                    break;
                }
            }
        }
        for (int i=0; i < stulist.length ; i++){
            if (stulist[i]==null){
                break;
            }
            System.out.println("第"+(i+1)+"名:"+stulist[i].score);
        }
    }

排序-按年龄排序

public void Sorting_age(){
        int temp = 0;
        for (int i = 0; i < stulist.length - 1; i++) {//轮数
            for (int j = 0; j < stulist.length - i - 1; j++) {   //每轮的次数a.Length - i - 1
                if (stulist[i+1]==null){
                    break;
                }
                if ( stulist[j].age > stulist[j+1].age){
                    temp = stulist[j].age;
                    stulist[j].age = stulist[j+1].age;
                    stulist[j+1].age = temp;
                }
            }
        }
        for (int i=0; i < stulist.length ; i++){
            if (stulist[i]==null){
                break;
            }
            System.out.println("第"+(i+1)+"名:"+stulist[i].score);
        }
    }

效果展示

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值