1
我想從0-11生成一個唯一的隨機數並使用它索引來指定TextBox一些獨特的字符。下面是代碼簡單的唯一隨機數生成循環錯誤JAVA Android
int[] previous_random = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};//initialized with invalid values
int current_random;
Random r_int = new Random();
boolean match = false;
for (int n=0;n<=11;n++)
{
gb[n] = (TextView)findViewById(IDs[n]);//assigning IDs from XML to java text boxes
}
int i = 0;
while (i<=11)
{
current_random = r_int.nextInt(11);//generating a random int from 0-11
int j =0;
while (j<=11)
{
if (current_random == previous_random[j])
{
match = true;break;//match = true shows that this random was used before to skip the loop
}
j++;
}
if(match == false)
{
gb[i].setText(randomized_final.charAt(current_random) + "\0");//randomized_final is a string not visible in current code
previous_random[i] = current_random;//*******PROBLEM HERE************
}
else
continue;
i++;
}
}
現在的問題是,標有** 問題就在這裏 **部分導致程序停止響應,並導致該應用程序去的CPU使用率超過50%,但應用程序不會崩潰,它只是顯示頂部欄的空白屏幕。這部分應該存儲新的獨特的隨機和存儲是爲了以後比較和如果我刪除這部分代碼工作正常,除了我得到重複隨機(這不是我想要的)。
請告訴我我做錯了什麼。或者告訴任何其他方法來做到這一點。我也嘗試過for循環。已經嘗試了幾個小時的替代方法!
這工作。非常感謝 :) – user2212736