从1-33号球中选取6个红球,且红球数字最多重复不超过3个 从1-16号球中选取一个篮球 由红球和蓝球共同组成一个双色球号码,且红球在左边(按照升序排列),篮球在右边。

    public static void main(String[] args) {
        //创建红色集合
        ArrayList<Integer> red = new ArrayList<>();
        int temp = 0;
        for (int i = 1; i <= 6; i++) {
            int num = (int) (Math.random() * 33 + 1);
            //collections类中的方法
            //int frequency(Collection,Object);返回指定集合中的指定元素出现的次数从0开始计数
            if (Collections.frequency(red, num) == 3) {
                //System.out.println("Collections.frequency(red, num)" + Collections.frequency(red, num));
                  i--;
                continue;
            } else {
                red.add(num);
            }
        }
        //System.out.println(red);
        //创建篮球集合
        ArrayList<Integer> bs = new ArrayList<>();
        int num = (int) (Math.random() * 16 + 1);
        bs.add(num);
        //升序排序
        Collections.sort(red);
        //定义集合接收
        ArrayList<Integer> ball = new ArrayList<>();
        //倒叙输出
        for (int i = red.size() - 1; i >= 0; i--) {
            ball.add(red.get(i));
        }
        ball.add(bs.get(0));//添加篮球
        System.out.println(ball);
    }
原文地址:https://www.cnblogs.com/zk2020/p/14090885.html