Java基础知识List练习作业—06

Java基础知识List练习作业—06
6. 写一个Student类, 包含属性id[1-30), grade[1-6], score[0-100], 所有属性都是随机生成
创建一个Set集合, 保存20个对象, 如果两个对象的id是一样的,则不允许添加.
使用迭代器遍历集合,打印输出对象的信息, 并找出分数最高的同学和分数最低的同学,
最后打印输出最高分和最低分同学信息.

/*6. 写一个Student类, 包含属性id[1-30), grade[1-6], score[0-100], 所有属性都是随机生成
   创建一个Set集合, 保存20个对象, 如果两个对象的id是一样的,则不允许添加.
   使用迭代器遍历集合,打印输出对象的信息, 并找出分数最高的同学和分数最低的同学, 
   最后打印输出最高分和最低分同学信息.
*/
package day15;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class HomeWork_006 {

        public static void main(String[] args) {
                Students[] stus = new Students[30];
                Set<Students> set = new TreeSet<Students>(new Comparator<Students>() {
                        Students stu1;
                        Students stu2;
                        @Override
                        public int hashCode() {
                                if(compare(stu1, stu2) == 0)
                                        return 0;
                                return super.hashCode();
                        }
                        @Override
                        public int compare(Students stu1, Students stu2) {
                                String ss1 = stu1.id+"";
                                String ss2 = stu2.id+"";
                                int res = ss1.compareTo(ss2);
                                if(res == 0) {
                                        return 0;
                                }else{
                                        return stu1.score - stu2.score;
                                }
                        }
                });
                int cut = 0;
               while(set.size()<30) {
                        int id = (int)(Math.random()*30+1);
                        int grade = (int)(Math.random()*6+1);
                        int score = (int)(Math.random()*100+1);
                        stus[cut] = new Students(id, grade, score);
                         if(set.add(stus[cut]))
                                 cut++;
                }
               Iterator<Students> it = set.iterator();
               while(it.hasNext()) {
                       it.next().print();
               }
               System.out.println("最低分:");
                ((TreeSet<Students>) set).first().print();
                System.out.println("最高分:");
                ((TreeSet<Students>) set).last().print();
        }

}

class Students {
        int id;
        int grade;
        int score;
        public Students(int id, int grade, int score) {
                super();
                this.id = id;
                this.grade = grade;
                this.score = score;
        }
        public void print() {
                System.out.println("学号: "+id+"\t年级: "+grade+"\t分数 \t: "+score);
        }
      
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值