Matrix without repeated numbers (java) !! Boolean method does not work!

1
public static void main(String[] args) {
        // TODO code application logic here

        int tauler[][] = new int[3][9];


        ordenarTauler(tauler);
        bombo(tauler);
        imprimirTauler(tauler);

    }

    public static void ordenarTauler(int[][] tauler) {

        Random rd = new Random();
        int a = 0;

        for (int i = 0; i < tauler.length; i++) {
            for (int j = 0; j < tauler[0].length; j++) {

                a = rd.nextInt(90) + 1;

                if (NumRepetitTauler(tauler, a) == false) {
                    tauler[i][j] = a;
                }
            }
        }

    }

    public static boolean NumRepetitTauler(int[][] tauler, int a) {

        boolean repetido = false;

        for (int i = 0; i < tauler.length; i++) {
            for (int j = 0; j < tauler[0].length; j++) {
                if (tauler[i][j] == a) {
                    repetido = true;
                    System.out.println(a);
                } else if (tauler[i][j] != a) {
                    repetido = false;
                }
            }

        }
        return repetido;
    }

    public static int bombo(int[][] tauler) {
        //possem els 0
        int bombo = 0;
        for (int i = 0; i < tauler.length; i++) {
            for (int j = 0; j < tauler[i].length; j++) {
                tauler[0][0] = bombo;
                tauler[1][0] = bombo;
                tauler[2][1] = bombo;
                tauler[0][2] = bombo;
                tauler[1][3] = bombo;
                tauler[2][3] = bombo;
                tauler[2][4] = bombo;
                tauler[0][5] = bombo;
                tauler[1][6] = bombo;
                tauler[2][6] = bombo;
                tauler[0][7] = bombo;
                tauler[1][8] = bombo;
            }
        }
        return bombo;
    }


    public static void imprimirTauler(int[][] tauler) {

        for (int i = 0; i < tauler.length; i++) {
            System.out.println("");
            for (int j = 0; j < tauler[0].length; j++) {
                System.out.print("   " + tauler[i][j]);
            }
            System.out.println("");
        }
    }

}
    
asked by Gerard Zafra Gonzlez 25.12.2018 в 13:12
source

1 answer

0

ok I corrected two things the first one is when assigning the values if it finds a repeated value the NumRepetitTauler method returns false then what you have to do is reassign a new random number and that's as easy as putting the counter in -1 so that it repeats again in the for and I also corrected the form in which you return the boolean in the method, you have to return True as soon as you find one repeated. I leave both methods corrected for you to analyze, greetings!

    public static void ordenarTauler(int[][] tauler) {
        Random rd = new Random();
        int a = 0;

        for (int i = 0; i < tauler.length; i++) {
            for (int j = 0; j < tauler[i].length; j++) {

                a = rd.nextInt(90) + 1;

                if (!NumRepetitTauler(tauler, a)) {
                    tauler[i][j] = a;
                }else{
                    j--;
                }
            }
        }
    }

    public static boolean NumRepetitTauler(int[][] tauler, int a) {

        for (int i = 0; i < tauler.length; i++) {
            for (int j = 0; j < tauler[i].length; j++) {
                if (tauler[i][j] == a) {
                    System.out.println(a);
                    return true;
                } 
            }
        }
        return false;
    }
    
answered by 25.12.2018 в 15:08