利用数组,随机生成五组 双色球 候选号红(
1-32
)
6
位
蓝
(1-16) 1
位
该代码利用for循环生成五组随机数组,引入Random类,产生随机数,把随机数控制在1-32。
rand.nextInt(1, 33)代码产生的随机数就是1到32,不包括33。
因为双色球号码不能重复,所以要遍历数组用if语句来判断是否有相同的数。
\033[31m 数 、033[0m 是用来改变颜色的31m-39m是用来控制字颜色的,31m为红色。
public static void main(String[] args) {
Random rand = new Random();
for (int x = 0; x < 5; x++) {
int[] reds = new int[6];
for (int i = 0; i < reds.length; i++) {
int ball = rand.nextInt(1, 33);
boolean f = true;
for (int n : reds) {
if (n == ball) {
f = false;
break;
}
}
if (f) {
reds[i] = ball;
} else {
--i;
}
}
Arrays.sort(reds);
int blue = rand.nextInt(1, 17);
// System.out.println("红:" + Arrays.toString(reds) + " 蓝:[" + blue + "]");
System.out.printf("\033[31m %02d %02d %02d %02d %02d %02d\033[0m \033[34m %02d \033[0m%n", reds[0], reds[1], reds[2], reds[3], reds[4], reds[5], blue);
}
}
结果如下:
感谢大家观看,希望能给大家带来帮助。