EJERCICIO 7


      Matrices: 

     Llenar una matriz donde capture por teclado el tamaño de la misma y esta solo acepte números pares.  Los resultados por teclado.
2
4
6
8
10
12
14
16
18



 package tercer.corte.pkg7;


import javax.swing.JOptionPane;

public class TercerCorte7 {

    public static void main(String[] args) {
        int[][] m;
        boolean flag;
        do {

            m = null;
            int f = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el numero de filas"));
            int c = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el numero de columnas"));
            m = new int[f][c];
            for (int i = 0; i < f; i++) {
                for (int j = 0; j < c; j++) {

                    do {

                        boolean flag2 = true;
                        while (flag2) {
                            int n = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese un numero"));
                            if ((n % 2) == 0) {
                                m[i][j] = n;
                                flag2 = false;
                            } else {
                                flag2 = true;
                                JOptionPane.showMessageDialog(null, "Ingrese solo numeros pares por favor");
                            }
                        }
                        flag = false;

                    } while (flag);
                }
            }

            for (int i = 0; i < f; i++) {//Fila
                for (int j = 0; j < c; j++) {//Columna

                    System.out.print(m[i][j] + "\t");
                }
                System.out.println("");
            }

            flag = false;
        } while (flag);
    }

}


Comentarios

Publicar un comentario

Entradas populares de este blog

EJERCICIO 2

EJERCICIO 3

EJERCICIO 5