我想創建一個程序來生成隨機數並存儲到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