当前位置:网站首页>创建一个长度为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");
}
}
边栏推荐
- How can I apply for a PMP certificate?
- 数据库多表关联查询问题
- Unity shader (to achieve a simple material effect with adjustable color attributes only)
- esp8266使用TF卡并读写数据(基于arduino)
- Yapi test plug-in -- cross request
- 【云原生】DevOps(一):DevOps介绍及Code工具使用
- SiteMesh getting started example
- Locust performance test 4 (custom load Policy)
- Sublime Text4 download the view in bower and set the shortcut key
- 数据建模中利用3σ剔除异常值进行数据清洗
猜你喜欢
随机推荐
JWT certification used in DRF
Interview question: general layout and wiring principles of high-speed PCB
PMP Exam Preparation experience systematically improve project management knowledge through learning
(3/8) method parameters of improper use of enumeration (2)
What is MD5
Locust performance test 5 (analysis)
PMP Exam details after the release of the new exam outline
網易雲微信小程序
Mysql:select ... for update
flex弹性布局
Colorbar of using vertexehelper to customize controls (II)
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
NATAPP内网穿透
Implementation of corner badge of Youmeng message push
Mysql database transaction learning notes
Cesium load vector data
Hard core sharing: a common toolkit for hardware engineers
Network request process
2020 year end summary
SiteMesh getting started example









