Imprimir varias matrices en la misma linea
Publicado por Davidmg91 (9 intervenciones) el 21/11/2010 12:57:24
Necesito ayuda para imprimir varias matrices en la misma línea tal que:
1 2 3 7 6 9 8 8 12
4 5 6 0 9 6 4 14 12
7 8 9 6 3 2 13 11 11
Donla la última matriz es la matriz de suma.
Tengo este código hecho pero se que no lo estoy haciendo bien porque no me sale como debería salir.
¿Podrían ayudarme a modificar el código para que salga bien?
Aquí el código: (La zona de asteriscos es la que no me sale bien)
import utilidades.Teclado;
public class Matriz {
public static void main(String[] args) {
// Pedimos el número de filas y de columnas
System.out.print("Diga el número de filas:");
int n = Teclado.readInt();
System.out.print("Diga el número de columnas:");
int m = Teclado.readInt();
// Creamos el array bidimensional
int [][] A = new int [n][m];
// Damos valores a la matriz
System.out.println("Ahora diga los valores de la matriz: ");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[i].length; j++){
A[i][j]= Teclado.readInt();
}
}
// Imprimimos la matriz
System.out.println("Matriz A:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(A[i][j]+" ");
}
System.out.println("");
}
// Creamos el segundo array, que tendrá valores aleatorios
int [][]y = new int [n][m];
// Damos valores aleatorios con la clase Math.Random
System.out.println("Matriz de valores aleatorios:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[i].length; j++){
int k = (int) (Math.random()*9);
y[i][j]= k;
}
}
// Imprimimos la matriz
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(y[i][j]+" ");
}
System.out.println("");
}
// Creamos el array matriz suma
int [][]suma = new int [n][m];
// Calculamos los valores de la matriz suma
for (int i=0; i<A.length; i++){
for (int j=0; j<A.length; j++){
int sumaM = A[i][j] + y[i][j];
suma[i][j]= sumaM;
}
}
// Imprimimos la matriz suma
System.out.println("Matriz suma:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(suma[i][j]+" ");
}
System.out.println("");
}
System.out.println();
***********************************************************************************************
// Imprimimos las 3 matrices juntas
System.out.println("Matrices juntas:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(A[i][j]+"\t" + y[i][j] + "\t" + suma[i][j] + "\t");
}
System.out.println("");
}
}
}
Muchas gracias de adelanto.
1 2 3 7 6 9 8 8 12
4 5 6 0 9 6 4 14 12
7 8 9 6 3 2 13 11 11
Donla la última matriz es la matriz de suma.
Tengo este código hecho pero se que no lo estoy haciendo bien porque no me sale como debería salir.
¿Podrían ayudarme a modificar el código para que salga bien?
Aquí el código: (La zona de asteriscos es la que no me sale bien)
import utilidades.Teclado;
public class Matriz {
public static void main(String[] args) {
// Pedimos el número de filas y de columnas
System.out.print("Diga el número de filas:");
int n = Teclado.readInt();
System.out.print("Diga el número de columnas:");
int m = Teclado.readInt();
// Creamos el array bidimensional
int [][] A = new int [n][m];
// Damos valores a la matriz
System.out.println("Ahora diga los valores de la matriz: ");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[i].length; j++){
A[i][j]= Teclado.readInt();
}
}
// Imprimimos la matriz
System.out.println("Matriz A:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(A[i][j]+" ");
}
System.out.println("");
}
// Creamos el segundo array, que tendrá valores aleatorios
int [][]y = new int [n][m];
// Damos valores aleatorios con la clase Math.Random
System.out.println("Matriz de valores aleatorios:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[i].length; j++){
int k = (int) (Math.random()*9);
y[i][j]= k;
}
}
// Imprimimos la matriz
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(y[i][j]+" ");
}
System.out.println("");
}
// Creamos el array matriz suma
int [][]suma = new int [n][m];
// Calculamos los valores de la matriz suma
for (int i=0; i<A.length; i++){
for (int j=0; j<A.length; j++){
int sumaM = A[i][j] + y[i][j];
suma[i][j]= sumaM;
}
}
// Imprimimos la matriz suma
System.out.println("Matriz suma:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(suma[i][j]+" ");
}
System.out.println("");
}
System.out.println();
***********************************************************************************************
// Imprimimos las 3 matrices juntas
System.out.println("Matrices juntas:");
for (int i=0; i<A.length; i++){
for (int j=0; j<A[0].length; j++){
System.out.print(A[i][j]+"\t" + y[i][j] + "\t" + suma[i][j] + "\t");
}
System.out.println("");
}
}
}
Muchas gracias de adelanto.
Valora esta pregunta


0