문제

I tried to write a program that would select teams to manage randomly,however when I run it I get the same 4 teams rather than different ones each time ?

I tried to make it so that each time I generate a random number it would go into an array.Then I would check against that array to see if the number had been used before.

Any help or advice would be appreciated. Thanks!

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);
                }
            }
        }
    }
}
도움이 되었습니까?

해결책

I "cleaned" your code a little bit and changed the Array to check duplicate random numbers to an ArrayList. It's not the fastest solution, but it should work.

The problem is that you don't get out of the for loop until the whole program exits. As geoand described above, RandInt == NA[k] will never be true, because RandInt will always be <= 19, so no new random number will be generated. So there are two wrong things in the code.

When you want to learn more about faster checking for duplicate entries, maybe this will help you: http://javarevisited.blogspot.de/2012/02/how-to-check-or-detect-duplicate.html

I hope I could help you. :)

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

다른 팁

The problem is that RandInt==NA[k] is never true (because RandomInt is at most 19 due to the size of places) and therefore RandomInt is never updated inside the for loop. The while loop is only executed once since the for loop seems to do all the work

Is seems like you need to rethink your random generation algorithm

My recommendation is to generate a random number inside the for loop as shown below

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top