我需要隨機獲取數字1-4並將其存儲在arrayList中。我有一個好的開始,但我可能會有一個無限循環,因爲模擬器不斷崩潰。這些數字也不能被使用一次以上。Android獲得4個隨機數
** lightSelector is a global variable (int lightSelector = 1;)
** generatedOrder is my ArrayList
for (int i = 0; i < 4; i++)
{
lightSelector = 1 + (int)(Math.random() * 4);
generatedOrder.add(lightSelector);
boolean contains = true;
System.out.print(generatedOrder.get(i));
if (!generatedOrder.isEmpty())
{
// Loop until a number is generated that hasn't already been picked
while (contains)
{
if (generatedOrder.contains(lightSelector))
lightSelector = 1 + (int)(Math.random() * 4);
else
{
generatedOrder.add(lightSelector);
contains = false;
break;
}
}
}
}
如果發生崩潰,您將得到一個logcat輸出。這是什麼意思? – 2013-03-25 03:49:40
打開跟蹤文件時出錯:沒有這樣的文件或目錄(2) – Andrew 2013-03-25 03:51:13
它會更長。大約10-30行。紅色或黃色。編輯它到您的問題 – 2013-03-25 03:52:15