首页 > 解决方案 > 如何用随机整数填充矩阵而不重复它们?

问题描述

菜鸟编程。我正在尝试编写宾果游戏,我遇到了两个我不知道如何应对的艰巨挑战(对我来说)。- 如何用不重复的整数填充我的二维数组。- 如何在宾果卡(矩阵)的中间留下空白。这就是它应该的方式。

这是我用来填充矩阵值的函数

public static void fillBoard(int [][] pBoard){

      Random rand = new Random();
       for (int[] pBoard1 : pBoard) {
           for (int j = 0; j < pBoard1.length; j++) {
               pBoard1[j] = rand.nextInt(100);
           }
       }


   }

这就是我初始化矩阵的方式

public static int [] [] defineBoard(){

        int [] [] board = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},
            {1,2,3,4,5},};

        return board;

    }

我这样打印

public static void printBoard(int [][] pBoard){

       for(int [] rows : pBoard) 
        {
            for(int column : rows) 
            {
                System.out.print("| " + column + " ");
            }
                System.out.println("|");
        }
}

这是我的输出示例

| 34 | 43 | 6 | 22 | 61 |
| 18 | 95 | 43 | 75 | 53 |
| 40 | 10 | 34 | 38 | 66 |
| 43 | 74 | 77 | 77 | 34 |
| 95 | 69 | 48 | 29 | 38 |

我的问题再次是:我不知道如何在中间(3d 行,3d 列)留下一个空白空间,我无法让这些值不重复。谢谢!

标签: java

解决方案


There are classes in the Java Platform that will make the completion of this task simpler. As GBloggett has pointed out, the HashSet class in the Java Collections Framework is defined so that the set may not contain duplicate values. Attempting to add an existing value to a HashSet results in the set being unmodified. Exploiting this contract, you can generate 25 unique random numbers this way:

Set<Integer> grid = new HashSet<Integer>();
Random rand = new Random();

while (grid.size() < 25) {
    Integer val = rand.nextInt(100);
    grid.add(val);
}

Note that the Java Collections Frameworks classes are generic types. The declaration new HashSet<Integer>() simply informs the compiler that your Set instance may contain only Integer instances.

Printing out the grid does not require a matrix. You can get a linear array directly from the HashSet instance and then exploit a little logic: namely that the grid row is equal to (array_index / 5) and that the grid column is equal to (array_index % 5). It is also true for your use case that the 'middle' of the grid is always (row, column) = (2, 2), so you can exploit this to make a hard coded solution for getting an empty space in the middle:

Integer[] gridInts = grid.toArray(new Integer[0]);
StringBuilder output = new StringBuilder();

for (int index = 0; index < gridInts.length; index++) {
   int row = index / 5;
   int col = index % 5;

   String cell = (row == 2 && col == 2) ? "--" : gridInts[index].toString();
   output.append(" | ").append(cell);
   if (col == 4) output.append(" |" + "\\n");  // causes a new line to start
                                               // after the last column in the grid
}

String result = output.toString();             // gets String from StringBuilder
System.out.println(result);                    // sends grid to standard output

These code snippets should address your needs. Note that the ternary operator is used to determine whether to generate a blank spot in the output. This is simply an in-line if-then-else expression. Note also that a StringBuilder instance is used to hold the output while it is being constructed. It isn't strictly necessary and the string concatenation operator could be used instead (and it might be more readable)


推荐阅读