输入:
输出:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[][] str = getArray();
for (int columnIndex = 0; columnIndex < 4; columnIndex++) {
System.out.println("Sun of the elements at column"
+columnIndex+ " is "+sumColumn(str,columnIndex));
}
}
public static double[][]getArray(){
double[][] str = new double[3][4];
Scanner input = new Scanner(System.in);
System.out.println("Enter a 3-by-4 matrix row bu row:");
for (int i = 0; i < str.length; i++) {
for (int j = 0; j < str[i].length; j++) {
str[i][j] = input.nextDouble();
}
}
return str;
}
public static double sumColumn(double[][] array,int columnIndex){
double sum = 0;
for (int i = 0; i < 3; i++) {
sum += array[i][columnIndex];
}
return sum;
}
}