[英]How to Make two arrays that randomizes numbers between 10 and 30, and 11 and 30 in Java
I have to make two arrays that randomizes numbers between 10 and 30(first array), 11 and 30(second array) in java.我必须制作两个数组,在 java 中随机化 10 到 30(第一个数组)、11 和 30(第二个数组)之间的数字。 i'm supposed to fill the first and second one in numerical order... how?
我应该按数字顺序填写第一个和第二个......如何? i haven't started yet and i need to be finished soon.
我还没有开始,我需要尽快完成。
int[] a = new int[20];
int random = (int)(Math.random()*21+10);
for(int x=0; x<a.length; x++){
System.out.print(random + " ");
}
System.out.println();
for(int i=11; i<=30; i++){
System.out.print(i + " ");
}
}
} }
you can use您可以使用
((Math.random() * ((max - min) + 1)) + min) to get random with limit
((Math.random() * ((max - min) + 1)) + min) 获得随机限制
int min = 10;
int max = 30;
int[] a = new int[20];
int[] b = new int[20];
for(int i =0;i<a.length;i++) {
a[i]= (int) ((Math.random() * ((max - min) + 1)) + min);
}
max = 31;
for(int i =0;i<b.length;i++) {
b[i]= (int) ((Math.random() * ((max - min) + 1)) + min);
}
this will do这会做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.