用java编写双色球程序
时间: 2023-08-28 15:03:24 浏览: 300
ssq.rar_ssq_双色球
以下是一个简单的Java代码示例,用于生成双色球的随机号码:
```java
import java.util.Arrays;
import java.util.Random;
public class DoubleColorBall {
public static void main(String[] args) {
int[] redBalls = generateRedBalls();
int blueBall = generateBlueBall();
System.out.println("红球:" + Arrays.toString(redBalls));
System.out.println("蓝球:" + blueBall);
}
private static int[] generateRedBalls() {
int[] redBalls = new int[6];
Random random = new Random();
for (int i = 0; i < 6; i++) {
int redBall = random.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (redBalls[j] == redBall) {
redBall = random.nextInt(33) + 1;
j = -1;
}
}
redBalls[i] = redBall;
}
Arrays.sort(redBalls);
return redBalls;
}
private static int generateBlueBall() {
Random random = new Random();
return random.nextInt(16) + 1;
}
}
```
该程序使用Random类生成随机数,其中generateRedBalls方法生成6个不重复的1到33之间的整数作为红球号码,generateBlueBall方法生成1到16之间的整数作为蓝球号码。最后,程序打印出生成的红球和蓝球号码。
阅读全文