为什么我的输出给了我错误数量的每个数字?

huangapple go评论70阅读模式
英文:

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

问题

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

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

英文:

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 &lt; list.length; i++) {
            for (int j = 0; j &lt; list[i].length; j++) {
                int x = (int)((Math.random()* max) + 1);
                int y = 0;

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

                if(count &lt; 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] + &quot; &quot;);
            }
            System.out.println();
        }

        return &quot;&quot;;
    }
}

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

答案1

得分: 0

你的方法

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

没有检查 ycount 是否已经达到了 most

英文:

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

答案2

得分: 0

你生成一个随机数。

然后,你检查这个随机数是否“无效”,也就是说,它已经被使用了太多次。

然后,你生成一个新的随机数,检查这个数是否与你之前的数相同,然后就使用这个新数。你没有检查这个数是否也“过载”。所以,这里可能发生的情况是,你的算法选择了“9”,计算了9的数量,发现有4个,然后生成了一个新的随机数,又是9,然后生成了另一个数,是4,然后直接将4放入,而不再次检查。

重新调整你的while循环。

或者,更好的做法是,创建一个实用程序类来将生成随机数(但不是已经返回了N次的数)的任务分担给一个单独的类,这样你就可以解开这段混乱的代码。

英文:

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.

答案3

得分: 0

你的问题在这里:

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

这基本上只是排除了 x 重复次数较多的情况,但并不包括 y。

另外,我建议使用哈希映射来跟踪出现次数,而不是一遍又一遍地迭代整个数组。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月23日 23:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64030961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定