获取10个20-35之间的随机数,要求不能重复
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public class Demo01 { public static void main(String[] args) { Set<Integer> set = new HashSet<>(); Random r = new Random(); int num = 0; while (num < 10){ int number = r.nextInt(35) + 1; //编写条件 当set中没有这个数 且 这个数大于等于20 if(!set.contains(number) && number >= 20){ set.add(number); num++; } } //foreach遍历输出 for (Integer integer : set) { System.out.print(" " + integer); } } }
显示结果
复制代码
1
2
334 35 20 21 23 25 26 27 29 31 Process finished with exit code 0
最后
以上就是高高雪碧最近收集整理的关于[Java]获取10个20-35之间的随机数,要求不能重复的全部内容,更多相关[Java]获取10个20-35之间内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复