hello,我现在想把几乎所有的东西都放在第二个function里,因为检查的时候只看第二个方法,不看main方法, 但是那样的话我就不会call 方法了,然后之前给double array赋的值也用不上了,不知道该怎么解决,求指点。谢谢
下面放的这个是可以跑成功的,但是我需要把if else都从main里移出去到第二个方法
public class MatrixMult {
public static void main(String[] args) {
double[][] firstMatrix = {{3.0, 2.0, 1.0},{1.0, 2.0, 3.0}};
double[][] secondMatrix = {{4.0, 5.0},{5.0, 4.0},{6.0, 5.0}};
if(firstMatrix[0].length != secondMatrix.length){
System.out.println("Warning!");
}
else{
double[][] result = multiplyMatrices(firstMatrix, secondMatrix);
for(int row = 0; row < result.length; row++){
for(int column = 0; column < result[0].length; column++){
System.out.print(result[row][column] + " ");
}
System.out.println();
}
}
}
public static double[][] multiplyMatrices(double[][] firstMatrix, double[][] secondMatrix){
double[][] result = new double[firstMatrix.length] [secondMatrix[0].length];
for(int row = 0; row < firstMatrix.length; row++){
for(int column = 0; column < secondMatrix[0].length; column++){
for(int i = 0; i < firstMatrix[0].length; i++){
result[row][column] += firstMatrix[row][i] * secondMatrix[i][column];
}
}
}
return result;
}
}
public static void main(String[] args) {
get();
}
private static void get() {
double[][] firstMatrix = {{3.0, 2.0, 1.0},{1.0, 2.0, 3.0}};
double[][] secondMatrix = {{4.0, 5.0},{5.0, 4.0},{6.0, 5.0}};
if(firstMatrix[0].length != secondMatrix.length){
System.out.println("Warning!");
}
else{
double[][] result = new double[firstMatrix.length] [secondMatrix[0].length];
for(int row = 0; row < firstMatrix.length; row++){
for(int column = 0; column < secondMatrix[0].length; column++){
for(int i = 0; i < firstMatrix[0].length; i++){
result[row][column] += firstMatrix[row][i] * secondMatrix[i][column];
}
}
}
for(int row = 0; row < result.length; row++){
for(int column = 0; column < result[0].length; column++){
System.out.print(result[row][column] + " ");
}
System.out.println();
}
}
}