我写了一个Java程序,最后怎么得到不重复的
importjava.util.Random;publicclassTest2{staticRandomrandom=newRandom();publicstaticvo...
import java.util.Random;
public class Test2 {
static Random random = new Random();
public static void main(String[] args) {
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] =(f(60, 100, 3));
System.out.println(a[i]);
}
}
public static int f(int fromS, int toS, int modeNum) {
int n = 0;
boolean flag = true;
while (flag) {
n = random.nextInt(toS - fromS + 1) + fromS;// random.nextiInt=(结束-起始+1)+起始
if (n % 3 == 0) {
flag = false;
}
}
return n;
}
} 展开
public class Test2 {
static Random random = new Random();
public static void main(String[] args) {
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] =(f(60, 100, 3));
System.out.println(a[i]);
}
}
public static int f(int fromS, int toS, int modeNum) {
int n = 0;
boolean flag = true;
while (flag) {
n = random.nextInt(toS - fromS + 1) + fromS;// random.nextiInt=(结束-起始+1)+起始
if (n % 3 == 0) {
flag = false;
}
}
return n;
}
} 展开
展开全部
HashSet<Integer> set = new HashSet<Integer>();
while(set.size()<10){
int randomNumber =(f(60, 100, 3));
set.add(randomNumber);
}
int[] a = new int[10];
for(int j=0;j<set.size();j++){
a[j]=i;
System.out.println(i);
}
main方法改成上面这个
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询