2014-05-03 87 views
0

我試圖編寫一個程序,選擇團隊隨機管理,但是當我運行它時,每次都得到相同的4個團隊而不是不同的團隊?隨機輸出,無法正常工作

我試圖讓它每次產生一個隨機數時都會進入一個數組。然後我會檢查該數組,看看數字是否曾經用過。

任何幫助或建議,將不勝感激。謝謝!

import java.util.*; 

class Start { 


    static String[] places = {"Man Utd", "Arsenal", "Aston Villa", "Chelsea", 
      "Everton", "Fulham", "Liverpool", "Man City", "Newcastle", "Norwich", 
      "QPR", "Reading", "Southampton", "Stoke", "Sunderland", "Swansea", 
      "Spurs", "West Brom", "West ham", "Wigan"}; 

    static int[] NA = {21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21}; 
    static Random rand = new Random(); 
    static int RandInt = 0; 
    static boolean x = false; 
    static boolean p = false; 
    static int player = 1; 

    public static void main(String[] args) { 

     while (x != true) { 

      RandInt = rand.nextInt(places.length); 
      for (int k = 0; k <= NA.length; k++) { 
       while (p != true) { 
        if (RandInt == NA[k]) { 
         RandInt = rand.nextInt(places.length); 
        } else { 
         p = true; 
         NA[k] = RandInt; 

        } 
       } 
       System.out.println("player " + player + " is managing " + places[RandInt]); 
       player++; 
       p = false; 
       if (player >= 5) { 
        x = true; 
        System.exit(0); 
       } 
      } 
     } 
    } 
} 
+1

int [] NA是什麼意思? – geoand

+0

如果您使用答案,請選擇它。 – NonSecwitter

回答

1

我「清理」了一下你的代碼,並且改變了數組來檢查重複的隨機數到一個ArrayList。這不是最快的解決方案,但它應該起作用。

問題是,在整個程序退出之前,您不會退出for循環。如上所述,RandInt == NA [k]永遠不會成立,因爲RandInt始終爲< = 19,因此不會生成新的隨機數。所以在代碼中有兩個錯誤的東西。

當你想了解更多更快的檢查重複條目,也許這將幫助你:http://javarevisited.blogspot.de/2012/02/how-to-check-or-detect-duplicate.html

我希望我可以幫助你。 :)

static String[] places = {"Man Utd", "Arsenal", "Aston Villa", "Chelsea", 
     "Everton", "Fulham", "Liverpool", "Man City", "Newcastle", "Norwich", 
     "QPR", "Reading", "Southampton", "Stoke", "Sunderland", "Swansea", 
     "Spurs", "West Brom", "West ham", "Wigan"}; 
static int[] NA = new ArrayList<Integer>(5); 
static Random rand = new Random(); 
static int RandInt = 0; 
static int player = 1; 

public static void main(String[] args) { 
    while (player < 5) { 
    RandInt = rand.nextInt(places.length); 

    for (int i = 0; i <= NA.size(); i++) { 
     if (RandInt == NA.get(i)) { 
     RandInt = rand.nextInt(places.length); 
     } else { 
     NA.add(RandInt); 
     break; 
     } 
    } 
    System.out.println("player " + player + " is managing " + places[RandInt]); 
    player++; 
    } 
    System.exit(0); 
} 
0

的問題是,RandInt==NA[k]是不正確的(因爲RandomInt至多19由於places大小),因此RandomInt從不更新裏面的for循環。因爲for循環似乎做所有的工作while循環只執行一次

是好像你需要重新考慮你的隨機生成算法

+0

RandInt在每次while循環迭代時更新 – NonSecwitter

+0

只有一次 - 檢查代碼流,你會看到 – geoand

+0

不,它會迭代4次,直到'player == 5'和'player'遞增並且在主'while'循環中測試 – NonSecwitter

0

我的建議是產生for循環內的隨機數,如圖低於

while (x != true) { 

      for (int k = 0; k <= NA.length; k++) { 
      RandInt = rand.nextInt(places.length); 
       while (p != true) { 
        if (RandInt == NA[k]) { 
         RandInt = rand.nextInt(places.length); 
        } else { 
         p = true; 
         NA[k] = RandInt; 

        } 
       } 
       System.out.println("player " + player + " is managing " + places[RandInt]); 
       player++; 
       p = false; 
       if (player >= 5) { 
        x = true; 
        System.exit(0); 
       } 
      }