java - 随机输出,无法正常工作

标签 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);
                }
            }
        }
    }
}

最佳答案

我稍微“清理”了你的代码,并更改了数组以检查 ArrayList 中的重复随机数。这不是最快的解决方案,但应该可行。

问题是在整个程序退出之前你无法退出 for 循环。如上所述,RandInt == NA[k] 永远不会为 true,因为 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);
}

关于java - 随机输出,无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23444706/

相关文章:

java - 具有多态性和工厂类的泛型

java - Libgdx 用图像填充屏幕

java - 具有 Avro 记录的 Kafka Streams TopologyTestDriver 的架构注册问题

mysql - 在 MySQL 中插入/更新随机日期

sql-server - SQL Server 选择随机和非随机

python - 随机选择 0 或 1 相同的次数?

java - 同步方法重写 - 线程获取哪个对象的锁?

java - 区分GridBagConstraints中的weightx和ipadx (Java)

c# - (Random.NextDouble() < 1) 的用途是什么

php - 从 PHP 关联数组中选择一个随机元素