设计一个Java程序,完成以下3个功能: (1)任意输入m和n,生成一个m*n的矩阵A,里面的数值由输入完成,输出A; (2)生成A的转置矩阵B,输出B; (3)由A与B相乘,得出矩阵C,输出C。
时间: 2024-09-26 12:03:27 浏览: 52
为了设计一个Java程序来完成上述三个任务,我们可以创建一个`Matrix`类,包含相关的操作方法。以下是程序的大致结构:
```java
import java.util.Scanner;
class Matrix {
private int[][] elements;
private int rows;
private int cols;
// 构造函数,接受行数和列数以及用户输入的元素
public Matrix(int m, int n) {
this.rows = m;
this.cols = n;
elements = new int[rows][cols];
Scanner scanner = new Scanner(System.in);
System.out.println("请输入矩阵A的元素:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
elements[i][j] = scanner.nextInt();
}
}
scanner.close();
}
// 方法用于获取矩阵A
public void display() {
for (int[] row : elements) {
for (int elem : row) {
System.out.print(elem + " ");
}
System.out.println();
}
}
// 转置矩阵的方法
public Matrix transpose() {
Matrix transposed = new Matrix(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed.elements[j][i] = elements[i][j];
}
}
return transposed;
}
// 相乘并返回结果矩阵C
public Matrix multiply(Matrix other) throws IllegalArgumentException {
if (cols != other.rows) {
throw new IllegalArgumentException("矩阵A的列数应等于矩阵B的行数");
}
Matrix result = new Matrix(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
int sum = 0;
for (int k = 0; k < cols; k++) {
sum += elements[i][k] * other.elements[k][j];
}
result.elements[i][j] = sum;
}
}
return result;
}
// 主方法来运行整个程序
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩阵A的行数和列数(例如: 3 4):");
int m = input.nextInt();
int n = input.nextInt();
Matrix matrixA = new Matrix(m, n);
Matrix transposeB = matrixA.transpose();
transposeB.display();
try {
Matrix matrixB = new Matrix(n, m); // B的构造函数需要m和n互换
Matrix matrixC = matrixA.multiply(matrixB);
matrixC.display();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)