当前位置:网站首页>创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
2022-07-07 06:48:00 【阿白|】
创建一个长度为6的int型数组,要求数组元素的值在1-30之间,且随机赋值。同时要求元素值各不相同。
要求1:random产生[ 0 , 1)的随机double型数据,乘以范围取整,得到数的范围为[左区间, 右区间 - 1],最后整体 + 1即为[左区间 + 1, 右区间]。
要求2:方法为用while循环刷随机数,这题还是比较简单的。
我测试了几遍自己的代码,没有超过1ms的,整体来说还是比较快的
package p1.text;
import java.util.*;
/**
* @author WitMoy
* @version V1.8
* @date : 2022-07-02 18:16
*/
public class Main {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
int[] ans = new int[6];
for(int i = 0; i < 6; i++){
ans[i] = (int)(Math.random()* 30 + 1);
while(true){
boolean flag = true;
for(int j = 0; j < i; j++){
if(ans[i] == ans[j]){
flag = false;
break;
}
}
if(flag) break;
ans[i] = (int)(Math.random()* 30 + 1);
}
}
System.out.println(Arrays.toString(ans));
long end = System.currentTimeMillis();
System.out.println("\n用时: " + (end - begin) + "ms");
}
}
边栏推荐
- 战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
- Jmeters use
- PMP experience learning and sharing process
- Detailed learning notes of JVM memory structure (I)
- Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
- C language pointer (special article)
- DRF defines views and routes
- What is the value of getting a PMP certificate?
- PMP Exam details after the release of the new exam outline
- 软件建模与分析
猜你喜欢
Locust performance test 4 (custom load Policy)
华为HCIP-DATACOM-Core_03day
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
C language pointer (Part 1)
What are the conditions for applying for NPDP?
Unity shader (learn more about vertex fragment shaders)
Confitest of fixture py
Skill review of test engineer before interview
【云原生】DevOps(一):DevOps介绍及Code工具使用
4、 Fundamentals of machine learning
随机推荐
Variable parameter of variable length function
(3/8)枚举的不当用法 之 方法参数(二)
消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
Network request process
What are the suggestions for PMP candidates?
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
软件建模与分析
The use of recycling ideas
Jenkins task grouping
Schema-validation: wrong column type encountered in column XXX in table XXX
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
Pycharm importing third-party libraries
Final keyword
How to speed up video playback in browser
Summary of PMP learning materials
What are the conditions for applying for NPDP?
信息安全实验二 :使用X-SCANNER扫描工具
12、 Sort
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes