Because it marks error of java.lang.ArrayIndexOutOfBoundsException: 3

0

I'm trying to make a magic picture but for some reason it marks me wrong         int n = 3;

    int i_a = 0;
    int j_b = 0;
    int P_I = n / 2;

    int[][] cuadro_m = new int[n][n];

    int temp = 1;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cuadro_m[i][j] = 0;
        }
    }
    int i = 0;
    int j = P_I;

    while (temp != (n * n) + 1) {
        if (cuadro_m[i][j] == 0) {
            cuadro_m[i][j] = temp;
        } else {
            i = i_a + 1;
            j = j_b;
            cuadro_m[i][j] = temp;

        }
        i_a = i;
        j_b = j;

        temp++;
        j++;
        i++;
        if (i < 0 && j == n) {
            i = n - 1;
            j = 0;

        } else if (i < 0) {
            i = i + n;
        } else if (j == n) {
            j = 0;

        }

    }

    for (int x = 0; x < n; x++) {
        String resultado = "";
        for (int y = 0; y <n; y++) {
            resultado = resultado + " " + cuadro_m[x][y];
        }
        System.out.println("" + resultado);
    }

}

}

    
asked by TGAB99 14.10.2018 в 08:16
source

0 answers