java 编程随机生成20个80以内的数,不允许有重复的数据,存放到数组中,最后输出 代码

急求

生成随机数可以java.util.Random类的nextInt(int)方法来生成,如果要不重复,可把这些数放入Set集合中,会自动去重。下面是代码:
import java.util.*;

public class Test
{
public static void main(String[] args)
{
Set<Integer> set=new HashSet<Integer>();
Random r=new Random();
do
{
int i=r.nextInt(80);
set.add(i);
}while(set.size()<20);
Integer[] arr=new Integer[20];
set.toArray(arr);
for(int a=0;a<arr.length;a++)
System.out.print(arr[a]+" ");
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-10
import java.util.Random;

public class Test  {
private int[] num = new int[20];
private int idx = 0;

public void push(int n) {
if (idx < 20)
num[idx++] = n;
}

public boolean hasExist(int n) {
for (int i = 0; i < idx; i++)
if (num[i] == n)
return true;
return false;
}

public int size() {
return idx;
}

public void print() {
for (int i = 0; i < idx; i++)
System.out.print(num[i] + " ");
System.out.print("\n");
}

public static void main(String[] args) {
Test t = new Test();
while (t.size() < 20) {
Random r = new Random();
int n = r.nextInt(101);
if (!t.hasExist(n))
t.push(n);
}
t.print();
}
}

第2个回答  2015-07-29
/**
* If the given graphs are isomorphic, returns a map between the vertex sets witnessing the isomorphism. If
* the given graphs are not isomorphic, returns null.
* @param a

* @param b
* @return
*/
public static Map<String, String> getIsomorphicMap(UndirectedGraph<String, DefaultEdge> a, UndirectedGraph<String, DefaultEdge> b) {

}
第3个回答  2015-07-13
童鞋你好!
这个估计需要自己搜索了!
网上基本很难找到免费给你服务的!
我在这里给你点搜索,
相似回答