用Java定义一个二维数组,从键盘输入一个矩阵的值,对矩阵进行转置,输出转置后的矩阵。并求出原矩阵中的最大值和最小值,输出其所在的行号和列号。(不要使用方法)
时间: 2024-06-03 11:11:14 浏览: 128
C语言数组二
好的,以下是解答:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入矩阵的行数和列数(用空格隔开):");
int row = scanner.nextInt();
int column = scanner.nextInt();
int[][] matrix = new int[row][column];
System.out.println("请输入矩阵的值:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println("\n原矩阵:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("\n转置后的矩阵:");
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int maxRow = 0, maxColumn = 0, minRow = 0, minColumn = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
maxRow = i+1;
maxColumn = j+1;
}
if (matrix[i][j] < min) {
min = matrix[i][j];
minRow = i+1;
minColumn = j+1;
}
}
}
System.out.println("\n最大值:" + max + ",所在行:" + maxRow + ",所在列:" + maxColumn);
System.out.println("最小值:" + min + ",所在行:" + minRow + ",所在列:" + minColumn);
}
}
希望解答能够帮到你,如果你还有其他问题,可以继续问我。
阅读全文