Java - Necesito AYUDA en un ejercicio de MATRICES en JAVA

 
Vista:
sin imagen de perfil

Necesito AYUDA en un ejercicio de MATRICES en JAVA

Publicado por anonymous (11 intervenciones) el 03/04/2022 01:01:51
EJPROG

Estoy hace 6 horas pensando como hacer este ejercicio y no me sale. Les agradecería un montón si me ayudan porque es para el lunes. Gracias!
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

Necesito AYUDA en un ejercicio de MATRICES en JAVA

Publicado por Billy Joel (876 intervenciones) el 03/04/2022 03:58:12
Gracias, estaba un poco complicado.
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 class Tarea2022Z_ZigZag {
 
    public static String[][] buildMatriz(String word, int rows) {
        String[][] matriz = new String[rows][word.length()];
        clear(matriz);
        boolean down = true;
        int j = 0;
        for (int i = 0; i < word.length(); i++) {
            matriz[j][i] = String.valueOf(word.charAt(i));
            if (down) {
                if (j + 1 < rows) {
                    j++;
                } else {
                    down = false;
                    j--;
                }
            } else {
                if (j - 1 >= 0) {
                    j--;
                } else {
                    down = true;
                    j++;
                }
            }
        }
        return matriz;
    }
 
    public static void clear(String[][] matriz) {
        for (String[] matriz1 : matriz) {
            for (int j = 0; j < matriz1.length; j++) {
                matriz1[j] = "";
            }
        }
    }
 
    public static void main(String[] args) {
        String word = "ABCDEFGHIJKL";
        int rows = 4;
//        String word = "PROGRAMAR";
//        int rows = 3;
        String[][] matriz = buildMatriz(word, rows);
 
        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz[i].length; j++) {
                System.out.print(matriz[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

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