本帖最后由 slay_alex92 于 2013-12-16 11:11 AM 编辑
我不太知道你的program的架構要怎麼寫
不過照你說的方式
我大概會用以下的方法
- String[] player = new String[8]; //做一個array,來放player,length自訂
- int[] list = new int[8]//用來儲存已經出現過的號碼,length跟上一個array一樣
- int remain = player.length; //取得還剩下多少player還沒被random到
- int listIndex = 0; //list的index
- public String randomPlayer () //回return random出來的player的名字
- {
- while(true)
- {
- int n = (int)(Math.random() * remain);
- boolean check = false; //用來判斷這個random出來的號碼之前有沒有出現過
- for(int i = 0; i < list.length; i++)
- {
- if(n == list[i])
- check = true; //如果出現過,把check改成true
- }
- if(! check) //如果還沒有出現過
- {
- list[listIndex] = n; //把這個數字加到list裡面,說明他這次出現過了,下次不要再選到
- listindex++;
- return player[n]; //return random出來的player的String
- }
- }
- }
复制代码因為我沒有你的code,所以沒有辦法測試哦~ 不過我寫的應該沒錯
另外這個方法是我臨時想出來的,有點複雜,說不定用ArrayList可以更快做出來