Java - Ayuda con esta matriz, no logro realizarla!

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 5 puestos en Java (en relación al último mes)
Gráfica de Java

Ayuda con esta matriz, no logro realizarla!

Publicado por Sergio (1 intervención) el 08/08/2018 09:16:02
Debe construir un programa que solicite un número N impar. Debe verificar que sea impar y no continuar hasta que se ingrese un número impar. Su programa debe construir una matriz de strings de tamaño NxN. Esa matriz será separada de forma virtual en 2 mitades, partiendo de posición fila 0, columna 0 y terminando en la posición fila N-1, columna N-1. La mitad de arriba debe ser llenada con números aleatorios convertidos a strings entre 0 y 99. La mitad de abajo debe ser llenada con números aleatorios convertidos a strings entre 100 y 199. La matriz resultante debe ser impresa en pantalla, tal y como se muestra a continuación:

Por favor ingrese un número entero: 5

56 85 4 21 183 31 72 90 126 174 80 5 151 190 127 25 148 119 102 170

Presione una tecla para continuar

Una vez que el usuario presione cualquier tecla, las posiciones de la mitad de arriba deben ser intercambiadas por la posición inversa de mitad de abajo.

183 126 151 148 56 174 190 119 85 31 127 102 4 72 80 170 21 90 5 25

No puede crear más matrices ni arreglos de ningún tipo para realizar su ejercicio.
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

Ayuda con esta matriz, no logro realizarla!

Publicado por Billy Joel (876 intervenciones) el 25/12/2018 17:20:13
Estaba buscando post sin respuesta y me encontré este... bueno siguiendo el enunciado lo resuelvo así:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
 
public class Sergio {
 
    public static Integer getRandomNumber(int min, int max) {
        return new Random().nextInt(max - min + 1) + min;
    }
 
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = 0;
        while (n % 2 == 0) {
            try {
                System.out.print("Introduzca un número impar: ");
                n = Integer.parseInt(br.readLine());
                if (n % 2 == 0) {
                    System.out.println("El número debe ser impar. Intente de nuevo ");
                }
            } catch (IOException ex) {
                System.out.println("Hubo un error de lectura, intente de nuevo");
                n = 0;
            }
        }
        String[][] matriz = new String[n][n];
        int centro = 0;
        for (int i = 0; i < matriz.length; i++) {
            if (i - (n - i - 1) == 0) {
                centro = i;
                break;
            }
        }
 
        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz[i].length; j++) {
                if (i < centro) {
                    matriz[i][j] = getRandomNumber(0, 99).toString();
                } else if (i == centro && j == centro) {
                    matriz[i][j] = "*";
                } else if (i >= centro) {
                    if (i == centro && j < centro) {
                        matriz[i][j] = getRandomNumber(0, 99).toString();
                    } else {
                        matriz[i][j] = getRandomNumber(100, 199).toString();
                    }
                }
            }
        }
 
        //Mostramos la matriz
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matriz[i][j] + "\t");
            }
            System.out.println();
        }
 
        try {
            System.out.println("Presione enter...");
            br.readLine();
        } catch (IOException ex) {
            ex.printStackTrace(System.out);
        }
 
        //Invertimos los valores de la matriz
        for (int i = 0; i < centro; i++) {
            for (int j = 0; j < n; j++) {
                String aux = matriz[i][j];
                matriz[i][j] = matriz[n - i - 1][j];
                matriz[n - i - 1][j] = aux;
            }
        }
 
        //Mostramos la matriz
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matriz[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Saludos!!
;-)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar