Sei sulla pagina 1di 7

27/11/12

MATRICES

Cargar una matriz de 3*3 Te permite ingresar los datos y luego los muestra por modo consola
import java.util.Scanner; public class suma { public static void main(String[] args) { // Creamos una matriz de 3x3 int matriz[][] = new int[3][3]; Scanner consola = new Scanner(System.in); // Recorremos la matriz para cargarla for (int x=0; x < matriz.length; x++) { for (int y=0; y < matriz[x].length; y++) { System.out.println("Introduzca el elemento [" + x + "," + y + "]"); matriz[x][y] = consola.nextInt(); } } // Listamos la matriz for (int x=0; x < matriz.length; x++) { for (int y=0; y < matriz[x].length; y++) { System.out.println ("[" + x + "," + y + "] = " + matriz[x][y]); } } } }

Ojo son 3 filas y 3 columnas quedara asi

Es decir asi seria su orden: [0,0] Fila o columna o Fila 1 Fila 2 [0,1] [0,2] 2 en columna 2 5 8 4 6 9

3 en columna 1 5 7

PROGRAMA QUE ME PERMITE INGRESAR DATOS ATRAVEZ DE UN CUADRO DE DIALOGO N MATRICES Y LA MUESTRA ATRAVEZ DE UN MENSAJE.(ES DECIR ALMACENA LOS DATOS DE UNA MATRIZ LA QUE ELIJAS Y LOS MUESTRA ATRAVEZ DE UN MENSAJE)

import javax.swing.JOptionPane; public class Matrizalmacena { public static void main(String args[]) { { // Definicin de una matriz int[][] matEnt; int fil = 0, col = 0; // Captura el nmero de filas y de columnas que va a contener la // matriz fil = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el nmero de Filas: ", "Matrizsuma",JOptionPane.QUESTION_MESSAGE)); col = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el nmero de Columnas: ", "Matrizsuma",JOptionPane.QUESTION_MESSAGE)); // Creacin de la matriz del nmero de filas y columnas que el // usuario // ingres matEnt = new int[fil][col]; // Cdigo encargado de capturar los datos a almacenar en la matriz for (int i = 0; i < col; i++) { for (int j = 0; j < fil; j++) { matEnt[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Dato fila[" + i + "] col ["+ j + "]", "Matrizsuma",JOptionPane.QUESTION_MESSAGE)); } } // Cdigo encargado de mostrar la matriz almacenada en memoria String matCadena = ""; for (int i = 0; i < col; i++) { for (int j = 0; j < fil; j++) { matCadena = matCadena + matEnt[i][j] + " "; } matCadena = matCadena + "\n"; } JOptionPane.showMessageDialog(null, matCadena, "Matrizsuma",JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // fin del mtodo main } // fin de la clase matricesV1 }

EJERCICICIO PROPUESTO EN CLASE


REALIZAR UN PROGRAMA QUE ME PERMITA SUMAR UNA MATRIZ DE 3X3
// Matrices int [][] matriz1 = {{2,4,4},{6,6,9}}; int [][] matriz2 = {{2,4,4},{6,6,9}}; int [][] matriz = new int[3][3];

REALIZAR UN PROGRAMA QUE ME PERMITA MULTIPLICAR UNA MATRIZ DE 3X3

Potrebbero piacerti anche