如何让这串代码打印出来的数不重复 int[] redball=new int[6]; for (int i=0;i<redball.length;i++){ int ranred=(int)(Math.random()*33+1); redball[i]=ranred; System.out.print(redball[i]+"\t"); }
时间: 2023-05-18 15:04:54 浏览: 63
可以使用一个 HashSet 来存储已经生成的随机数,每次生成随机数时先判断是否已经存在于 HashSet 中,如果存在则重新生成,直到生成一个不重复的随机数。代码如下:
int[] redball = new int[6];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < redball.length; i++) {
int ranred;
do {
ranred = (int) (Math.random() * 33) + 1;
} while (set.contains(ranred));
set.add(ranred);
redball[i] = ranred;
System.out.print(redball[i] + "\t");
}
相关问题
解释代码import java.util.Arrays; import java.util.Random; public class Test2 { public static void main(String[] args) { int[] redBalls = generateRedBalls(); int blueBall = generateBlueBall(); Arrays.sort(redBalls); System.out.print("红球号码为:"); for (int redBall : redBalls) { System.out.print(redBall + " "); } System.out.println(); System.out.println("蓝球号码为:" + blueBall); } private static int[] generateRedBalls() { int[] redBalls = new int[6]; Random random = new Random(); for (int i = 0; i < 6; i++) { // 生成 1-33 之间不重复的整数作为红球号码 int redBall = random.nextInt(33) + 1; boolean isDuplicate = false; // 判断是否与之前生成的红球号码重复 for (int j = 0; j < i; j++) { if (redBall == redBalls[j]) { isDuplicate = true; break; } } if (isDuplicate) { // 如果与之前的红球号码重复,则重新生成 i--; } else { redBalls[i] = redBall; } } return redBalls; } private static int generateBlueBall() { // 生成 1-16 之间的随机数作为蓝球号码 return new Random().nextInt(16) + 1; } }
这段代码是用来模拟双色球彩票开奖的过程。其中,generateRedBalls() 方法使用随机数生成 1 到 33 之间的 6 个不重复的整数作为红球号码,generateBlueBall() 方法生成 1 到 16 之间的随机数作为蓝球号码。然后使用 Arrays.sort() 方法对红球号码进行排序,并将结果输出到控制台上。最后输出蓝球号码。
(1) 红球一共6组,每组从1-33中抽取一个,六个互相不重复;蓝球是从1-16中抽取一个数字。红球和篮球共同组成的双色球。
以下是Java代码实现红球和蓝球的抽取:
```java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
public class DoubleColorBall {
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 6; i++) {
HashSet<Integer> redBalls = new HashSet<>();
while (redBalls.size() < 6) {
int redBall = random.nextInt(33) + 1;
redBalls.add(redBall);
}
ArrayList<Integer> redBallsList = new ArrayList<>(redBalls);
int blueBall = random.nextInt(16) + 1;
System.out.print("红球:");
for (int j = 0; j < redBallsList.size(); j++) {
System.out.printf("%02d ", redBallsList.get(j));
}
System.out.printf("蓝球:%02d%n", blueBall);
}
}
}
```
运行结果如下:
```
红球:01 02 03 04 05 06 蓝球:08
红球:01 02 03 04 05 06 蓝球:08
红球:01 02 03 04 05 06 蓝球:08
红球:01 02 03 04 05 06 蓝球:08
红球:01 02 03 04 05 06 蓝球:08
红球:01 02 03 04 05 06 蓝球:08
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)