Java 写一个随机双色球 的代码
时间: 2024-03-01 19:52:43 浏览: 121
好的,以下是使用 Java 编写的随机双色球代码:
```java
import java.util.Arrays;
import java.util.Random;
public class RandomDoubleColorBall {
public static void main(String[] args) {
int[] redBalls = generateRedBalls();
int blueBall = generateBlueBall();
System.out.println("红色球号码:" + Arrays.toString(redBalls));
System.out.println("蓝色球号码:" + blueBall);
}
// 生成红色球
public 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;
redBalls[i] = redBall;
for (int j = 0; j < i; j++) {
if (redBall == redBalls[j]) { // 判断是否重复
i--;
break;
}
}
}
Arrays.sort(redBalls); // 对红球号码进行排序
return redBalls;
}
// 生成蓝色球
public static int generateBlueBall() {
Random random = new Random();
int blueBall = random.nextInt(16) + 1;
return blueBall;
}
}
```
运行结果如下:
```
红色球号码:[7, 12, 18, 20, 28, 29]
蓝色球号码:10
```
阅读全文