双色球

红球33选6,篮球16选1

import java.util.Arrays;
import java.util.Random;

public class TestDoubleColor {
    public static void main(String[] args) {
        int []r=readyArray(33);
        int []b=readyArray(16);
        System.out.println(Arrays.toString(r));
        System.out.println(Arrays.toString(b));
        /**
         * 从红球号码数组选择6个保存到变量red
         */
        int[] red=selectRed(r,6);
        /**
         * 从篮球号码数组选择1个保存到变量blue
         */
        int blue=selectBlue(b);
        System.out.println(Arrays.toString(red));
        System.out.println(blue);
    }

    private static int selectBlue(int[] b) {
        /**
         * [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
         */
        return b[new Random().nextInt(b.length)];
    }

    private static int[] selectRed(int[] r,int select) {
        /**
         * r
         * [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
         *                       j
         * [false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,false,false,
         * false,false,false,false,false,false,false,false,false,false,false]
         * red 长度为6
         * [8, ]
         *  i
         */
        //保存结果的数组result
        int []result=new int[select];
        //标记数组 r.length=传入的值,这里是33
        boolean[] flag=new boolean[r.length];
        //遍历red数组,放入选择的红球号码,red.length=传入的值,这里是6
        for(int i=0;i

运行结果

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[23, 26, 7, 4, 5, 3]
3

你可能感兴趣的:(双色球)