用java写一个从1-33个数字里面随机选出7个不能重复的数字且从小到大排列,和从1-16个数字里面选出4个不能重复的数字且从小到大排列
时间: 2023-05-28 13:04:03 浏览: 68
import java.util.Arrays;
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
// 从1-33个数字里面随机选出7个不能重复的数字且从小到大排列
int[] numbers1 = new int[7];
int index1 = 0;
Random random = new Random();
while (index1 < 7) {
int num = random.nextInt(33) + 1;
if (!contains(numbers1, num)) {
numbers1[index1] = num;
index1++;
}
}
Arrays.sort(numbers1);
System.out.println(Arrays.toString(numbers1));
// 从1-16个数字里面选出4个不能重复的数字且从小到大排列
int[] numbers2 = new int[4];
int index2 = 0;
while (index2 < 4) {
int num = random.nextInt(16) + 1;
if (!contains(numbers2, num)) {
numbers2[index2] = num;
index2++;
}
}
Arrays.sort(numbers2);
System.out.println(Arrays.toString(numbers2));
}
private static boolean contains(int[] numbers, int num) {
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == num) {
return true;
}
}
return false;
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)