当前位置:网站首页>创建一个长度为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");
}
}
边栏推荐
- Some pit avoidance guidelines for using Huawei ECS
- Using JWT to realize login function
- What are the suggestions for PMP candidates?
- Common short chain design methods
- Zen - batch import test cases
- 12、 Sort
- Regular matching starts with XXX and ends with XXX
- Selenium mouse sliding operation event
- shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
- Interview question: general layout and wiring principles of high-speed PCB
猜你喜欢

Locust performance test 5 (analysis)

【云原生】DevOps(一):DevOps介绍及Code工具使用

Huawei hcip datacom core_ 03day

Full link voltage test of the e-commerce campaign Guide

Locust performance test 2 (interface request)

Integer or int? How to select data types for entity classes in ORM

Jenkins modifies the system time

答案在哪里?action config/Interceptor/class/servlet

Common short chain design methods

Information Security Experiment 2: using x-scanner scanning tool
随机推荐
【云原生】DevOps(一):DevOps介绍及Code工具使用
Common short chain design methods
MySql数据库-索引-学习笔记
华为HCIP-DATACOM-Core_03day
Interface test API case, data and interface separation
Mysql:select ... for update
信息安全实验三 :PGP邮件加密软件的使用
esp8266使用TF卡并读写数据(基于arduino)
软件建模与分析
浏览器中如何让视频倍速播放
Locust performance test 5 (analysis)
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
Some pit avoidance guidelines for using Huawei ECS
四、机器学习基础
Leetcode question brushing record (array) combination sum, combination sum II
信息安全实验一:DES加密算法的实现
信息安全实验四:Ip包监视程序实现
Network request process
C language pointer (special article)
超十万字_超详细SSM整合实践_手动实现权限管理