简体   繁体   English

在二维数组的列中查找重复数字

[英]Finding repeating numbers within a column in a 2D array

I'm making a digital Bingo board using numbers and I want all the numbers to be unique.我正在使用数字制作数字宾果板,我希望所有数字都是唯一的。 So I'm trying to create an algorithm that will loop through the column, find repeating numbers and then replace them with a random number between two certain numbers.所以我试图创建一个算法,它将遍历列,找到重复的数字,然后用两个特定数字之间的随机数替换它们。

This is what I have so far:这是我到目前为止所拥有的:

public void bingoBoardGenrerator(int[][] bingoBoard) {
    for (int row = 0; row < bingoBoard.length; row++) {
        int random = (int) (Math.random() * 15) + 1;
        bingoBoard[row][0] = random;
        for (int j = 1; j < bingoBoard.length; j++) {
            if (bingoBoard[j][0] == random) {
                bingoBoard[row][0] = (int) (Math.random() * 15) + 1;
            }
        }

The output should look like this but without the repeating numbers in the columns: output 应如下所示,但列中没有重复数字:

 Free space = 0

B   I   N   G   O   

5   25  36  53  61  
9   19  36  60  62  
1   17  0   54  63  
6   20  37  57  71  
5   19  39  57  69  

Any help would be really appreciated!任何帮助将非常感激!

For the "B" column:对于“B”列:

  1. Create an ArrayList with the Integers 1-15.使用整数 1-15 创建ArrayList
  2. Then use the Collections.sort() to randomize the numbers.然后使用Collections.sort()随机化数字。
  3. Then take the first 5 numbers from the ArrayList and add them to your Array然后从ArrayList中取出前 5 个数字并将它们添加到您的Array

Repeat the above steps for the I, N, G, O columns each using the appropriate range of numbers.使用适当的数字范围对 I、N、G、O 列重复上述步骤。

Or another approach (for the B) is to:或者另一种方法(对于 B)是:

  1. Create a Set.创建一个集合。
  2. Generate a random number in the range 1-15生成1-15范围内的随机数
  3. Add the number to the set将号码添加到集合中
  4. If the set contain 5 number you are finished.如果集合包含 5 个数字,您就完成了。 If not, repeat steps 2 - 4.如果没有,请重复步骤 2 - 4。

Repeat the above steps for the other columns.对其他列重复上述步骤。

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

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