用java代码写一个机选双色球
import java.util.Arrays; import java.util.Random;
public class RandomDoubleColorBall { public static void main(String[] args) { int[] redBalls = new int[33]; // 红球范围 1~33 for (int i = 0; i < redBalls.length; i++) { redBalls[i] = i + 1; }
int[] blueBalls = new int[16]; // 蓝球范围 1~16
for (int i = 0; i < blueBalls.length; i++) {
blueBalls[i] = i + 1;
}
int[] selectedRedBalls = new int[6]; // 选中的红球
for (int i = 0; i < selectedRedBalls.length; i++) {
int index = new Random().nextInt(redBalls.length - i);
selectedRedBalls[i] = redBalls[index];
redBalls[index] = redBalls[redBalls.length - 1 - i];
}
int selectedBlueBall = blueBalls[new Random().nextInt(blueBalls.length)]; // 选中的蓝球
Arrays.sort(selectedRedBalls); // 对红球进行排序
System.out.print("红球:");
for (int redBall : selectedRedBalls) {
System.out.print(redBall + " ");
}
System.out.println("蓝球:" + selectedBlueBall);
}
}
原文地址: https://www.cveoy.top/t/topic/L8L 著作权归作者所有。请勿转载和采集!