lunes, 4 de octubre de 2010

UNA MATRIZ EN JAVA CON SCANNER


ESTA ES UNA SENCILLA AYUDA PARA QUIEN QUIERA EN JAVA LLENAR Y MOSTAR UNA MATRIZ, ADEMAS UTILIZO UN SCANNER PARA LEER DESDE LA CONSOLA.
PUEDEN CORTAR Y PEGAR EN SU PACKAGE Y FUNCIONARA SIN PROBLEMAS.
public class Main {
    public static Scanner EN = new Scanner(System.in);
    public static int i,j;
    public static void main(String[] args) {
        int M, N;
        System.out.println("inserte la cantidad de filas");
        M = EN.nextInt();//CON SCANNER JAVA  LEEMOS
        System.out.println("inserte la cantidad de columnas");
        N = EN.nextInt();//CON SCANNER JAVA  LEEMOS

        System.out.println("inserte los datos");

        int mat[][] = new int[M][N];//DECLARAMOS E INICIALIZAMOS UNA MATRIZ EN JAVA

        llenar(mat);//LLAMAMOS AL METODO LLENAR Y LE PASAMOS UNA MATRIZ EN JAVA
System.out.println("La Matriz queda asi!!");

        mostrar(mat);//LLAMAMOS AL METODO MOSTRAR Y LE PASAMOS UNA MATRIZ
    } 
//EL METODO EN JAVA PARA LLENAR
    private static void llenar(int[][] mat) {
       for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                mat[i][j] = EN.nextInt();//EL OBJETO DE TIPO SCANNER LEE
            }
        }
    }
//EL METODO EN JAVA PARA MOSTRAR
    private static void mostrar(int[][] mat) {
        for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                System.out.print(mat[i][j] + " ");
            }
            System.out.println();
        }
    }   
}
LUEGO DE INGRESAR LAS CANTIDADES PARA FILA Y COLUMNA, PEDIRA LOS DATOS
DE LA MATRIZ A LLENAR , POR ULTIMO MOSTRARA EN ORDEN Y EN FORMATO DE MATRIZ.
LA SALIDA POR CONSOLA DEBERA SER ASI:

2 comentarios:

  1. Como son las cosas. Tengo que rendir con cierto profesor al que aqui han hecho alusion (que no voy a decir el nombre, solo que empieza con "Rod" y termina con "riguez") y he venido a parar aqui.
    Tan cierto jaja. Y justita la info. Exactamente lo que andaba buscando. Muchisimas gracias

    ResponderEliminar
  2. Bastante interesante :D Pero he encontrado un ejercicio con una interfaz grafica, un ejercicio de calculadora de matriz en java. Espero que también les sirva, como a mi. Saludos

    ResponderEliminar

/*===============medicion============================*/