int [] r = {1,21,13,44,59,2,6,17,86,13,……50,45};
中产生10个不重复的随机数
import java.util.Random;
public class Test {
public static int [] r = {1,21,13,44,59,2,6,17,86,13,50,45};
int a[]=new int[10];
public static void main(String[] args)
{ Test test=new Test();
Random rd=new Random();
for( int i=0;i<10;i++)
{
test.a[i]=rd.nextInt(r.length);
if(i>0&&test.process(i))
;
else i--;
}
}
public boolean process(int i)
{ boolean t=true;
for(int j=0;j<i;j++)
{
if(a[j]==a[i])
{
t=false;
return t;
}
}
return t;
}
}
上面的错了。
import java.util.Random;
public class Test {
public static int [] r = {1,21,13,44,59,2,6,17,86,13,50,45};
int a[]=new int[10];
public static void main(String[] args)
{ Test test=new Test();
Random rd=new Random();
for( int i=0;i<10;i++)
{
test.a[i]=r[rd.nextInt(r.length)];
if(i>0)
if(!test.process(i))
i--;
}
for(int i=0;i<10;i++)
System.out.println(test.a[i]);
}
public boolean process(int i)
{ boolean t=true;
for(int j=0;j<i;j++)
{
if(a[j]==a[i])
{
t=false;
return t;
}
}
return t;
}
}