3 回答

TA貢獻1829條經(jīng)驗 獲得超7個贊
通過改組列表,您可以將 0 到 49 之間的所有數(shù)字隨機放置在列表中:
questionNos.clear(); // if not empty
for (int i = 0; i < 50; i++) {
questionNos.add(i);
}
Collections.shuffle(questionNos);

TA貢獻1843條經(jīng)驗 獲得超7個贊
使用這段代碼
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random randomGenerator = new Random();
while (numbers.size() < 50) {
int random = randomGenerator.nextInt(50); // will generate a random number from 0 to 50
if (!numbers.contains(random)) { //will check whether the number is repeated or not
numbers.add(random); //if number is not repeated then it will add it in array
}
}

TA貢獻1848條經(jīng)驗 獲得超2個贊
您的updateQuestion().
for (int i = 0; i < 50; i++) {
questionNos.add(i);
}
因此,如果questionNos之前電話中已有 49 個問題,updateQuestion()您將收到 99 個問題,其中 98 個問題是重復(fù)的,依此類推。
把它移到只被調(diào)用一次的地方,比如構(gòu)造函數(shù)。
添加回答
舉報