Java - Select 来自 9x9 网格中每个正方形的数字

Java - Select a number from every square in a 9x9 grid

我有一个 9x9 的数独网格,我需要从网格中的每个 3x3 方格中获取一个随机数。

最糟糕的代码是这样的:

    if(square == 0) {
        row = random.nextInt(3);
        col = random.nextInt(3);
    }

    if(square == 1) {
        row = random.nextInt(3);
        col = random.nextInt(3) + 3;
    }
    if(square == 2) {
        row = random.nextInt(3);
        col = random.nextInt(3) + 6;
    }
    if(square == 3) {
        row = random.nextInt(3) + 3;
        col = random.nextInt(3);
    }
    if(square == 4) {
        row = random.nextInt(3) + 3;
        col = random.nextInt(3) + 3;
    }
    if(square == 5) {
        row = random.nextInt(3) + 3;
        col = random.nextInt(3) + 6;
    }
    if(square == 6) {
        row = random.nextInt(3) + 6;
        col = random.nextInt(3);
    }
    if(square == 7) {
        row = random.nextInt(3) + 6;
        col = random.nextInt(3) + 3;
    }
    if(square == 8) {
        row = random.nextInt(3) + 6;
        col = random.nextInt(3) + 6;
    }

其中 square 是方格在网格中的索引 (square = 0,1,...,8)

我想不出更好的写法。

一些想法?谢谢

这是一次所有事情的代码

public class Test {
    public static void main(String[] args) {
        //initializing arrays
        int[][] grid = new int[9][9];
        int[] numbers = new int[9];

        //populating grid
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                grid[i][j] = (int)(Math.random()*10);
            }
        }

        //printing grid
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }

        System.out.println();

        int counter = 0;
        //first and second loops counts for 0,3,6
        for(int i = 0; i < 9; i += 3) {
            for(int j = 0; j < 9; j += 3) {
                //after taking i or j values(which must be either 0,3 or 6) goes for 3x3 parts and saving those numbers in number array
                for(int t = i; t < i+3; t++) {
                    for(int k = j; k < j+3; k++) {
                        numbers[counter] = grid[t][k];
                        counter++;
                    }
                }

                //you can pick randomly from numbers array here

                //showing which numbers picked
                for(int t = 0; t < 9; t++) {
                    System.out.print(numbers[t]);
                }
                System.out.println();
                System.out.println();
                counter = 0;
            }
        }
    }
}

像这样

int[] rowAdd = new int[] { 0, 0, 0, 3, 3, 3, 6, 6, 6 };
int[] colAdd = new int[] { 0, 3, 6, 0, 3, 6, 0, 3, 6 };
row = random.nextInt(3) + rowAdd[square];
col = random.nextInt(3) + colAdd[square];

放入一个数组值,将其添加到变量row,命名为rowAdd。在第二个数组 colAdd 中放置应添加到 col.
的变量 最后,像索引一样使用 square 来获取正确的加法值。

当然,数组rowAddcolAdd应该是方法的一部分。 (每次调用该方法时都需要创建新数组,这会占用大量时间和内存)。这些数组应该是 class 相关的,所以它们应该是 static

这应该适用于任何正方形尺寸。你的情况是 3x3,所以大小是 3。

int size = 3;
row = random.nextInt(size) + (square / size) * size;
col = random.nextInt(size) + (square % size) * size;