Java随机一注双色球代码

如果这里输出的号能中奖,那程序员可发达了。

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

public class DoubleBalls {
public static void main(String[] args) {
String[] pools = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33" };
boolean[] used = new boolean[pools.length];
String[] ball = new String[6];
Random rd = new Random();
int i = 0;
while (true) {
int index = rd.nextInt(pools.length);
if (used[index]) {
continue;
}
ball[i] = pools[index];
used[index] = true;
i++;
if (i == ball.length) {
break;
}
}
Arrays.sort(ball);// 对前6个红球进行排序
ball = Arrays.copyOf(ball, ball.length + 1); // 将ball的数组长度有6变成7
ball[ball.length - 1] = pools[rd.nextInt(16)];// 从pools的数组中前16个元素随机选择一个代表篮球
System.out.println(Arrays.toString(ball));
}
}

Leave a Reply