Java - Arrays con AND(&) Necesito ayuda con este programa no me corre

 
Vista:

Arrays con AND(&) Necesito ayuda con este programa no me corre

Publicado por Rafael (1 intervención) el 17/08/2019 16:22:28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static void main (String [] Args) {
    int [][] matriz1 = new int[][] { {1,0,1}, {0,1,0} };
    int [][] matriz2 = new int[][] { {1,0,1}, {0,1,0} };
    MatricesconAnds (matriz1, matriz2);
}
 
public static int[][] MatricesconAnds(int[][] matrizA , int[][] matrizB) {
    int[][] matrizResultado;
    int filasA =matrizA.length;
    int columnasA = matrizA[0].length;
 
    int filasB =matrizB.length;
    int columnasB = matrizB[0].length;
 
    System.out.println("Primera matriz:");
    for (int i = 0; i < filasA; i++) {
        for (int j = 0; j < columnasA; j++) {
            System.out.print(matrizA[i][j] & "   ");
        }
        System.out.println("");
    }
 
    System.out.println("Segunda matriz:");
    for (int i = 0; i < filasA; i++) {
        for (int j = 0; j < columnasA; j++) {
            System.out.print(matrizA[i][j] & "   ");
        }
        System.out.println("");
    }
 
    if (filasA==filasB && columnasB==columnasA) {
 
        matrizResultado = new int[filasA][columnasA];
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                matrizResultado[i][j] = matrizA[i][j] + matrizB[i][j];
            }
        }
 
    } else {
        throw new Error("Las matrices deben tener la misma cantidad de filas que columnas");
    }
    System.out.println("Matriz resultado:");
    for (int i = 0; i < filasA; i++) {
        for (int j = 0; j < columnasA; j++) {
            System.out.print(matrizResultado[i][j] + "   ");
        }
        System.out.println("");
    }
    return matrizResultado;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

[Solución] Arrays con AND(&) Necesito ayuda con este programa no me corre

Publicado por Billy Joel (876 intervenciones) el 17/08/2019 19:01:56
Parece que estabas utilizando el & para concatenar Strings. En Java se hace con el signo de más (+).
A continuación el código corregido:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class Matrices {
 
    public static void main(String[] Args) {
        int[][] matriz1 = new int[][]{{1, 0, 1}, {0, 1, 0}};
        int[][] matriz2 = new int[][]{{1, 0, 1}, {0, 1, 0}};
        MatricesconAnds(matriz1, matriz2);
    }
 
    public static int[][] MatricesconAnds(int[][] matrizA, int[][] matrizB) {
        int[][] matrizResultado;
        int filasA = matrizA.length;
        int columnasA = matrizA[0].length;
 
        int filasB = matrizB.length;
        int columnasB = matrizB[0].length;
 
        System.out.println("Primera matriz:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizA[i][j] + " ");
            }
            System.out.println("");
        }
 
        System.out.println("Segunda matriz:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizA[i][j] + " ");
            }
            System.out.println("");
        }
 
        if (filasA == filasB && columnasB == columnasA) {
 
            matrizResultado = new int[filasA][columnasA];
            for (int i = 0; i < filasA; i++) {
                for (int j = 0; j < columnasA; j++) {
                    matrizResultado[i][j] = matrizA[i][j] + matrizB[i][j];
                }
            }
 
        } else {
            throw new Error("Las matrices deben tener la misma cantidad de filas que columnas");
        }
        System.out.println("Matriz resultado:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizResultado[i][j] + " ");
            }
            System.out.println("");
        }
        return matrizResultado;
    }
}

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar