繁体   English   中英

随机输出,无法正常工作

[英]Random Output, not working correctly

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

我稍微“清理”了你的代码并更改了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);
}

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

好像你需要重新考虑你的随机生成算法

我的建议是在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);
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM