import java.util.*;
class Ran
{
public static boolean biJiao(int[] arr,int a)
{
boolean b = true;
for (int y=0; y<arr.length;y++ )
{
if (arr[y]==a)
{
b=false;
break;
}
}
return b;
}
public static void main(String[] args)
{
int [] arr1 =new int[52];
Random ran = new Random();
for (int x=0;x<arr1.length;)
{
int a =ran.nextInt(52);
boolean b =biJiao(arr1,a);
if (b)
{
arr1[x]=a;
x++;
}
}
}
}
//1.产生52个0-52之间的随机数,不能重复!
public class RandomNumber{
public static void main(String[] args) {
Set<Integer> set = new HashSet<Integer>();
while (true) {
if (set.size() == 52) {
break;
}
int num = new Random().nextInt(52);
set.add(num);
}
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
}作者: 杨卫腾 时间: 2012-9-2 17:49
楼上的方法可行,运用集合的方法就是挺叼! 可以大大提高效率,可以简化代码!
class RandomTest
{
public static void main(String[] args)
{
Set<Integer> set = new HashSet<Integer>();
Random ran = new Random();
while(set.size()<52)
{
int num = ran.nextInt(52);
set.add(num);
}
for(Iterator<Integer> it = set.iterator(); it.hasNext(); )