2014-05-03 90 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); 
       } 
      }