java 随机5住双色球 代码
时间: 2023-03-12 10:55:15 浏览: 57
答:根据你的要求,这里有一段 Java 代码可以实现: Random random = new Random();
int[] redBall = new int[6];
int[] blueBall = new int[1];for (int i=0; i<6; i++) {
redBall[i] = random.nextInt(34) + 1;
for (int j=0; j<i; j++) {
if (redBall[i] == redBall[j]) {
i--;
}
}
}blueBall[0] = random.nextInt(17) + 1;System.out.print("红球:");
for (int i=0; i<6; i++) {
System.out.print(redBall[i] + " ");
}
System.out.println();
System.out.println("蓝球:" + blueBall[0]);
相关问题
Java 写一个随机双色球 的代码
好的,以下是使用 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
```
使用Java代码模拟双色球随机选号
好的,以下是使用Java代码模拟双色球随机选号的示例代码:
import java.util.Arrays;
import java.util.Random;
public class DoubleColorBall {
public static void main(String[] args) {
int[] redBalls = new int[6];
int blueBall;
Random random = new Random();
for (int i = 0; i < 6; i++) {
redBalls[i] = random.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (redBalls[i] == redBalls[j]) {
i--;
break;
}
}
}
blueBall = random.nextInt(16) + 1;
Arrays.sort(redBalls);
System.out.print("红球:");
for (int i = 0; i < 6; i++) {
System.out.print(redBalls[i] + " ");
}
System.out.println("蓝球:" + blueBall);
}
}
这段代码会随机生成6个1到33之间的不重复的数字作为红球,以及一个1到16之间的数字作为蓝球,并将它们输出到控制台上。
希望这个代码对你有所帮助!