Java怎么从键盘上输入矩阵
时间: 2023-05-19 14:04:08 浏览: 52
您可以使用 Scanner 类来从键盘上输入矩阵。以下是一个示例代码:
import java.util.Scanner;
public class MatrixInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
System.out.println("Enter the matrix elements: ");
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println("The matrix is: ");
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](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)