随机输出,无法正常工作

时间:2014-05-03 12:55:06

标签: java random

我尝试编写一个程序来选择团队随机管理,但是当我运行它时,我每次都得到相同的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);
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我&#34;清洁&#34;您的代码稍微改变了Array以检查重复的随机数到ArrayList。它不是最快的解决方案,但应该可行。

问题是,在整个程序退出之前,你不会退出for循环。正如上面描述的geoand,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);
}

答案 1 :(得分:0)

问题是RandInt==NA[k]永远不会成立(因为RandomInt的大小places最多为19个),因此RandomInt永远不会更新环。 while循环只执行一次,因为for循环似乎完成了所有的工作

您似乎需要重新考虑随机生成算法

答案 2 :(得分: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);
                }
            }