简体   繁体   中英

Why does my output give me the wrong amount of each number?

I am trying to make a program where I can input the size of a 2D array, the highest number in a 2D array, and the most amount of a certain number in the 2D array, and then fill it with random numbers in between 1 and the highest number. In my code, I specify that the max amount of times a number should repeat is 4, yet my output doesn't match that. Any suggestions?

This is my code:

class Main {
    public static void main(String[] args) {
        System.out.println(fill(6, 9, 4));
    }

    public static String fill(int size, int max, int most) {
        int[][] list = new int[size][size];

        int count = 0;

        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[i].length; j++) {
                int x = (int)((Math.random()* max) + 1);
                int y = 0;

                count = 0;
                for (int k = 0; k < list.length; k++) {
                    for (int l = 0; l < list[k].length; l++) {
                        if(list[k][l] == x) count++;
                    }
                }

                if(count < most) {
                    list[i][j] = x;
                } else {
                    while(true) {
                        y = (int)((Math.random()* max) + 1);
                        if(y != x) break;
                    }
                    list[i][j] = y;
                }

                System.out.print(list[i][j] + " ");
            }
            System.out.println();
        }

        return "";
    }
}

And this is my output:

9 4 6 1 9 1 
7 1 4 4 3 2 
6 1 4 2 7 9 
5 9 4 7 2 5 
3 5 3 5 7 4 
3 8 8 6 2 6 

Problem: There are 6 "4"s and 2 "8"s

Your method

while(true) {
    y = (int)((Math.random()* max) + 1);
    if(y != x) break;
}

does not check that count of y did not already reached most

You generate a random number.

You then check if this random number is 'invalid', in the sense that it's been used too many times.

Then, you generate a new random number, check that this isn't the same as your previous number, and then just roll with that . You are failing to check if this number, too, is 'overloaded'. So, what could have happened here is that your algorithm picked '9', counts 9s, finds 4 of them, rolls up a new random number, 9 again, so it rolls yet another number, 4, and just puts 4 in, without checking again.

Rejigger your while loops.

Or, better yet, make a utility class to offload the job of generating a random number, but not a number that's already been returned N times, to a separate class, so that you can untangle this messy code.

Your Issue is here:

while(true) {
    y = (int)((Math.random()* max) + 1);
    if(y != x) break;
}
list[i][j] = y;

This basically just rules out that x will be repeated more than most, but not y.

On a side note, I recommend using hash maps to keep track of the occurrences instead of iterating over the whole array over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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