【发布时间】:2016-02-27 21:01:53
【问题描述】:
所以我正在尝试制作一个程序,该程序生成一个包含 20 个随机数的数组,并且没有重复项(对于最终用户)。到目前为止,这是我的代码
import java.util.*;
public class randomprog
{
public static void main(String args[])
{
Random rand = new Random();
int[] list = new int[20];
boolean generating=true;
int counting=0;
while(generating)
{
int testNum= rand.nextInt(30)+1;
if (Arrays.asList(list).contains(testNum))
{}
else
{
list[counting]=testNum;
counting++;
System.out.println(testNum);
}
if(counting>=20)
{
generating=false;
}
}
}}
如您所见,我已经尝试过使用 Arrays.asList(list).contains(mynumber) 但是我仍然在输出中收到重复的内容,例如 29 4 4 1 20 30 20 23 30 11 6 7 27 14 16 8 4 19 7 15
有什么建议吗?
【问题讨论】: