0
嗯,我想添加兩個矩陣,但我的數學是錯誤的,我想我沒有想法如何得到它的權利。我覺得絕對愚蠢,我能不知道這一點,但我的頭好痛,我全力以赴的選項代數:添加兩個矩陣
import java.util.Scanner;
public class AddMatrices
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int N = 3;
System.out.print("Enter matrix1: ");
double[][] matrix1 = new double[3][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = input.nextDouble();
}
}
System.out.print("Enter matrix2: ");
double[][] matrix2 = new double[3][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix2[i][j] = input.nextDouble();
}
}
double[][] resultMatrix = addMatrix(matrix1, matrix2);
System.out.println("The addition of the matrices is ");
printResult(matrix1, matrix2, resultMatrix, '+');
}
public static double[][] addMatrix(double[][] m1, double[][] m2)
{
double[][] result = new double[m1.length][m1[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++)
m1[i][j] += m2[i][j];
}
return result;
}
public static void printResult(double[][] m1, double[][] m2, double[][] m3, char op)
{
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[0].length; j++) {
System.out.print(" " + m1[i][j]);
}
if (i == m1.length/2)
System.out.print(" " + op + " ");
else {
System.out.print(" ");
}
for (int j = 0; j < m2[0].length; j++) {
System.out.print(" " + m2[i][j]);
}
if (i == m1.length/2)
System.out.print(" = ");
else {
System.out.print(" ");
}
for (int j = 0; j < m3[0].length; j++) {
System.out.print(" " + m3[i][j]);
}
System.out.println();
}
}
}
預期的結果是什麼,你得到的結果是什麼?另外,散步,奇蹟清除頭。 – MikeTheLiar
@mikeTheLiar +1散步。我通常會像瘋子一樣在我的房間裏跳來跳去:) – Cruncher
結果假設是矩陣1,2的統一打印輸出,以及添加它們的結果。我的打印效果全部瘋狂 – majortom84