public void randomThird(){
int i = 1;
Random random = new Random();
int[] id =new int[3];
id[0]=random.nextInt(1000);
while(i<3){
if(id[i] != random.nextInt(1000)){
id[i] = random.nextInt(1000);
}else{
continue;
}
i++;
}
for (int j : id) {
System.out.println(j);
}
}
楼主这样做不对,你这样生成的只是相邻的两个随机数不会相同,如果不相邻还是会可
能相同的;
楼主可以参考下:
package project;
import java.util.Random;
public class Test {
public static void main(String[] args)
{
int a[] = new int[10];
int i = 0;
Random random = new Random();
while(i < 10){
int m = 0;
a[i] = random.nextInt(100);
for(int j = 0;j <= i;j++)
if(a[i] == a[j]){
m = 1;
break;
}
if(m == 1)
continue;
i++;
}
for (int j : a) {
System.out.println(j);
}
}
}
public void randomThird(){
Random random = new Random();
int a=random.nextInt(1000);
System.out.println(a);
int i=1;
while(i<3){
int k=random.nextInt(1000);
if(a!=k&&k!=0){
System.out.println(k);
}else{
continue;
}
i++;
}
}
互相学习
楼主,你的third()方法好像有点问题...你把代码中的“1000”改为“10”,运行会出现重复数字!