我想创建一个程序来生成随机数并存储到arraylist
。 同时获取存储的号码multiply
。随机数存储在数组列表中,并从中选择
我有存储生成的号码,但我怎么才能真正得到我想要的号码。 对于这个例子,我试图得到3rd number
,我错过了哪一步?
public class arraylist {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
int numtogen;
int third;
Scanner scan = new Scanner(System.in);
System.out.println("How many number do you want to generate: ");
numtogen = scan.nextInt();
System.out.println("What number do you want to multiply with the third number: ");
third = scan.nextInt();
HashSet<Integer> generated = new HashSet<Integer>();
// Prevent repeat
for (int x = 1; x <= numtogen; ++x) {
while (true) {
// generate a range from 0-100
int ranNum = rand.nextInt(100);
if (generated.contains(ranNum)) {
continue;
} else {
list.add(ranNum);
System.out.println("Number " + x + "=" + " = " + ranNum);
break;
}
}
}
int numinlist;
while (!list.isEmpty()) {
// Integer[] numlist= numbinlist.hasNextInt;
// int answer = numlist[2]*third;
// System.out.println("Answer to first number = "+answer);
}
}
}
你想做什么是相当不清楚。 – Zyerah 2013-04-24 07:04:08
第三个随机数只是一个随机数。 List或Set是多余的,所以你可以做'int triple = rand.nextInt(100)* 3;'不管它们输入什么(我假设这只是一个练习;) – 2013-04-24 07:04:40