Le programme que j'ai écrit multiplie une matrice 3 par 3. Il demande à l'utilisateur d'entrer la matrice a et la matrice b puis il affiche son produit. J'ai fait ceci cependant mon seul problème est que la sortie imprime seulement le produit que je voudrais pour qu'il imprime matrix a * matrix b = product(a,b)
. J'ai essayé de mettre System.out.println(a[i][j]+ " ");
au-dessus du System.out.print(mul[i][j]+"");
mais cela salit toute la sortie et la multiplication.Impression de matrices a et b
//this is my code:
import java.util.Scanner;
public class Matrices
{
public static double[][] multiplyMatrix(double[][] a,double[][] b)
{
double c[][]=new double[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
return c;
}
public static void main(String args[])
{
//Create Scanner object to read input from user
Scanner sc=new Scanner(System.in);
double a[][]=new double[3][3];
double b[][]=new double[3][3];
double sum[][]=new double[3][3];
double mul[][]=new double[3][3];
//Read the elements of matrix b
System.out.println("Enter the elements of matrix a:");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=sc.nextInt();
System.out.print(a[i][j]+"")
//Read the elements of Matrix b
System.out.println("Enter the elements of matrix b:");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b[i][j]=sc.nextInt();
//Call the method multiplyMatrix to multiply a and b
mul=multiplyMatrix(a,b);
System.out.println("Multiplication of two matrices:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(mul[i][j]+"");
}
System.out.println();
}
}
}
qu'il imprime dans une colonne comment puis-je obtenir à imprimer dans une rangée – Andrey
S'il vous plaît être plus précis lorsque vous postez la question. – Vidhi