当前位置:网站首页>创建一个长度为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");
}
}
边栏推荐
- Do you have any certificates with high gold content?
- Detailed learning notes of JVM memory structure (I)
- 超十万字_超详细SSM整合实践_手动实现权限管理
- What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
- Regularly modify the system time of the computer
- Cesium load vector data
- golang select机制和超时问题怎么解决
- **grafana安装**
- 嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
- PMP Exam Preparation experience systematically improve project management knowledge through learning
猜你喜欢
随机推荐
Schema-validation: wrong column type encountered in column XXX in table XXX
Zen - batch import test cases
信息安全实验四:Ip包监视程序实现
Regularly modify the system time of the computer
Windows starts redis service
2020 year end summary
Test Engineer Interview Questions 2022
消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
网易云微信小程序
E-commerce campaign Guide
软件建模与分析
Jenkins+ant+jmeter use
Skill review of test engineer before interview
The use of recycling ideas
Interview question: general layout and wiring principles of high-speed PCB
信息安全实验一:DES加密算法的实现
Leetcode刷题记录(数组)组合总和、组合总和 II
flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗
Jemter operation
战略合作|SubQuery 成为章鱼网络浏览器的秘密武器








