- 32
- 0
- 约6.61万字
- 约 95页
- 2021-09-03 发布于广东
- 举报
《Java语言程序设计(基础篇)》(第10版 梁勇 著)
第八章 练习题答案
8.1
public class Exercise08_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(Enter a 3 by 4 matrix row by row: );
double[][] m = new double[3][4];
for (int i = 0; i m.length; i++)
for (int j = 0; j m[i].length; j++)
m[i][j] = input.nextDouble();
for (int j = 0; j m[2].length; j++) {
System.out.println(Sum of the elements at column + j + is +
sumColumn(m, j));
}
}
public static double sumColumn(double[][] m, int columnIndex) {
double total = 0;
for (int i = 0; i m.length; i++)
total += m[i][columnIndex];
return total;
}
}
8.1附加
public class Exercise08_01Extra {
public static void main(String[] args) {
final int N = 2;
Scanner input = new Scanner(System.in);
double[][] A = new double[N][N];
System.out.print(Enter a, b, c, d: );
A[0][0] = input.nextDouble();
A[0][1] = input.nextDouble();
A[1][0] = input.nextDouble();
A[1][1] = input.nextDouble();
double[][] inverseA = inverse(A);
if (inverseA == null)
System.out.println(No inverse matrix);
else
printMatrix(inverse(A));
}
public static void printMatrix(double[][] A) {
for (int i = 0; i A.length; i++) {
for (int j = 0; j A.length; j++)
System.out.print(A[i][j] + );
System.out.println();
}
}
public static double[][] inverse(double[][] A) {
double[][] result = new double[A.length][A.length];
double determinant = A[0][0] * A[1][1] - A[0][1] * A[1][0];
if (determinant == 0)
return null;
result[0][0] = A[1][1] / determinant;
result[0][1] = -A[0][1] / determinant;
result[1][0] = -A[1][0] / determinant;
result[1][1] = A[0][0] / determinant;
return result;
}
}
8.2
public class Exercise08_02 {
public static void main(String[] args) {
Scanner input = ne
原创力文档

文档评论(0)