题目
题目描述与运行示例
8.1(求矩阵中各列数字的和)使用下面的方法头编写一个方法,求矩阵中特定列的所有元素的和:
public static double sumColumn(double[][] m, int columnIndex)
编写一个测试程序,读取3*4矩阵,显示每列元素的和
运行示例:
Enter a 3-by-4 matrix row by row:
1.5 2 3 4
5.5 6 7 8
9.5 1 3 1
Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0
破题
- 主方法:声明一个长度为3*4的double型矩阵
- 主方法:使用双层循环读取控制台输入为矩阵赋值
- 主方法:创建一个int型对象columnIndex,赋值为0
- 主方法:创建一个double对象,用于接收方法返回值
- 主方法:循环表头,columnIndex从0开始到3结束
- 主方法:调用sumColumn方法,传入矩阵和列数,使用刚刚创建的double对象接收返回值
- 主方法:输出列和
- sumColumn方法:创建一个int对象获取行数,double型对象保存加和
- sumColumn方法:通过循环计算列和
- sumColumn方法:返回列和
代码
import java.util.Scanner;
public class Test8_1 {
public static void main(String[] args) {
//1. 主方法:声明一个长度为3*4的double型矩阵
double[][] m = new double[3][4];
//2. 主方法:使用双层循环读取控制台输入为矩阵赋值
Scanner input = new Scanner(System.in);
System.out.println("Enter a 3-by-4 matrix row by row:");
for (int a = 0 ; a < 3 ; a++){
for (int b = 0 ; b < 4 ; b++){
m[a][b] = input.nextDouble();
}
}
//3. 主方法:创建一个int型对象columnIndex,赋值为0
int columnIndex = 0;
//4. 主方法:创建一个double对象,用于接收方法返回值
double result = 0;
//5. 主方法:循环表头,columnIndex从0开始到3结束
for (columnIndex = 0 ; columnIndex < 4 ; columnIndex++){
//6. 主方法:调用sumColumn方法,传入矩阵和列数,使用刚刚创建的double对象接收返回值
result = sumColumn(m, columnIndex);
//7. 主方法:输出列和
System.out.print("Sum of the elements at column " + columnIndex + " is " + result + "\n");
}
}
public static double sumColumn(double[][] m, int columnIndex){
//8. sumColumn方法:创建一个int对象获取行数,double型对象保存加和
int row = m.length;
double sum = 0;
//9. sumColumn方法:通过循环计算列和
for (int i = 0 ; i < row ; i++){
sum += m[i][columnIndex];
}
//10.sumColumn方法:返回列和
return sum;
}
}
版权声明:本文为weixin_46356698原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。