为什么我的矩阵在Java中无法正常工作?

时间:2018-11-26 18:39:13

标签: java arrays matrix intellij-idea multidimensional-array

下面是我的矩阵代码,我想用唯一的数字填补空白。但是我不知道为什么它不起作用。我尝试逐步进行操作,但似乎无法弄清楚自己犯了什么错误。所以,问题是,我有一个给定的9 x 9矩阵,里面有一些数字,我想用唯一的数字来完成其余的矩阵,以便每一行和每一列都有从1到9的所有数字,但每个数字都不同列/行。 请帮助我,谢谢!

package com.javagement;

import java.util.Random;

public class Main {

    public static void main(String[] args) {

        int[][] matriceBuni = new int[9][9];

        for(int i = 0; i < 9; i++){
            for(int j = 0; j < 9; j++){
                matriceBuni[i][j] = 0;
            }
        }

        matriceBuni[0][0] = 8;
        matriceBuni[0][4] = 6;
        matriceBuni[0][8] = 4;
        matriceBuni[1][2] = 7;
        matriceBuni[1][4] = 3;
        matriceBuni[1][8] = 5;
        matriceBuni[2][0] = 3;
        matriceBuni[2][1] = 9;
        matriceBuni[2][5] = 5;
        matriceBuni[2][7] = 8;
        matriceBuni[3][2] = 8;
        matriceBuni[3][3] = 6;
        matriceBuni[3][6] = 7;
        matriceBuni[3][7] = 5;
        matriceBuni[4][2] = 5;
        matriceBuni[4][3] = 8;
        matriceBuni[4][7] = 9;
        matriceBuni[5][1] = 6;
        matriceBuni[5][2] = 3;
        matriceBuni[5][5] = 7;
        matriceBuni[5][6] = 4;
        matriceBuni[6][0] = 7;
        matriceBuni[6][1] = 3;
        matriceBuni[6][3] = 2;
        matriceBuni[6][5] = 9;
        matriceBuni[7][0] = 1;
        matriceBuni[7][1] = 8;
        matriceBuni[7][7] = 5;
        matriceBuni[7][8] = 9;
        matriceBuni[8][4] = 8;
        matriceBuni[8][5] = 1;
        matriceBuni[8][6] = 2;

        for(int m = 0; m < 9; m++) {
            for(int n = 0; n < 9; n++) {
                System.out.print(matriceBuni[m][n] + " ");
            }
            System.out.println();
        }

        System.out.println("*****************************************");



        for(int r = 0; r < 9; r++) {
            for(int c = 0; c < 9; c++) {
                if(matriceBuni[r][c] == 0) {
                    boolean isUnique = false;
                    while(!isUnique) {
                        int uniqueNumber = getRandomNumberInRange(1,9);
                        int countUnique = 0;
                        for(int a = 0; a < 9; a++) {
                            if (matriceBuni[r][a] == uniqueNumber) {
                                countUnique++;
                            }
                        }
                         for(int b = 0; b < 9; b++) {
                             if (matriceBuni[b][c] == uniqueNumber) {
                                 countUnique++;
                             }
                         }
                        if (countUnique == 0) {
                            isUnique = true;
                            matriceBuni[r][c] = uniqueNumber;
                        }
                    }
                }
            }
        }



        for(int m = 0; m < 9; m++) {
            for (int n = 0; n < 9; n++) {
                System.out.print(matriceBuni[m][n] + " ");
            }
            System.out.println();
        }
    }


    private static int getRandomNumberInRange(int min, int max) {

        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }

}

0 个答案:

没有答案