Java 矩阵数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13694534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Matrices Arrays
提问by user1368970
This program adds two 3x3 matrices. It compiles and runs, but the output, instead of being:
该程序添加了两个 3x3 矩阵。它编译并运行,但输出,而不是:
1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2
It produces:
它产生:
1.0 2.0 3.0 0.0 2.0 4.0 0.0 0.0 0.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 0.0 0.0 0.0
7.0 8.0 9.0 1.1 4.3 5.2 0.0 0.0 0.0
I'm not sure why the output displays as all zeros? The math in the program 'seems' right to me... Is there something I'm missing here?
我不确定为什么输出显示为全零?程序中的数学对我来说“似乎”正确......我在这里遗漏了什么吗?
import java.util.Scanner;
public class AddMatrices{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int N = 3;
//get the users input and store it in the two arrays
System.out.println("\nEnter matrix1: \n");
//declare 2 arrays with the appropriate number of rows and columns in
//them to store the numbers in each matrix.
//this is the first one.
double[][] matrix1 = new double[N][N];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = input.nextDouble();
}
}
//get the users input and store it in the two arrays
System.out.println("\nEnter matrix2: \n");
//declare 2 arrays with the appropriate number of rows and columns in
//them to store the numbers in each matrix.
//this is the second one.
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();
}
}
//call the addMatrix method and pass it the two arrays
double[][] resultingMatrix = addMatrix(matrix1, matrix2);
System.out.println("The addition of the matrices is ");
}//end of main method
//write the addMatrix method to add the two matrices and display the result
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];
}
}
for (int i = 0; i < m1.length; i++) {
char plus = '+';
for (int j = 0; j < m1[0].length; j++) {
System.out.print(" " + m1[i][j]);
}
if (i == m1.length / 2)
System.out.print(" " + plus + " ");
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 < result[0].length; j++) {
System.out.print(" " + result[i][j]);
}
System.out.println();
}
return result;
}//end of add matrices
}//end of class
回答by awolfe91
You're setting the added values to m1 instead of result. In your first double-for loop, just do:
您将添加的值设置为 m1 而不是结果。在您的第一个双循环中,只需执行以下操作:
result[i][j] = m1[i][j]+m2[i][j];
回答by Salchi13
I think you are never assigning the result to the variable results
我认为您永远不会将结果分配给变量结果
you should change
你应该改变
m1[i][j] += m2[i][j];
to
到
result[i][j]=m1[i][j] + m2[i][j];