如何找到矩阵中最大和最小数量的确切位置。该代码可以显示矩阵中的最大值和最小值,但我需要找到确切的位置,例如行和列。需要像这样格式化。最大数目为2 0行,列3如何查找矩阵中最大和最小数量的确切位置
import java.util.Scanner;
public class alarconh_Program2 {
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int row = 0;
int col = 0;
double col1sum,col2sum,col3sum,col4sum,col1avg,col2avg,col3avg,col4avg;
System.out.println(" (Col) x (Row) ");
System.out.println("Enter the number of rows");
row = input.nextInt();
System.out.println("Enter the number of columns");
col = input.nextInt();
double [][] matrix = new double[row][col];
for (int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
matrix [i][j] = input.nextDouble();
}
}
System.out.println();
String formathd="The Matrix is";
System.out.printf(formathd);
for (int i1 = 0; i1 < matrix.length; i1++){
System.out.println();
for(int j1 = 0; j1 < matrix[i1].length; j1++){
String Table=" %2.1f ";
System.out.printf(Table, matrix [i1][j1]);
}
}
col1sum = matrix[0][0] + matrix[1][0] + matrix[2][0];
col2sum = matrix[0][1] + matrix[1][1] + matrix[2][1];
col3sum = matrix[0][2] + matrix[1][2] + matrix[2][2];
col4sum = matrix[0][3] + matrix[1][3] + matrix[2][3];
System.out.println();
String SUM = "%2.1f %2.1f %2.1f %2.1f :SUM";
System.out.printf(SUM,col1sum,col2sum,col3sum,col4sum);
col1avg= col1sum/row;
col2avg = col2sum/row;
col3avg = col3sum/row;
col4avg = col4sum/row;
System.out.println();
String AVG = " %2.1f %2.1f %2.1f %2.1f :AVG";
System.out.printf(AVG,col1avg,col2avg,col3avg,col4avg);
System.out.println();
double maxValue = Integer.MIN_VALUE;
for (int i2 = 0; i2 < matrix.length; i2++)
for (int j2 = 0; j2 < matrix[i2].length; j2++)
if (matrix[i2][j2] > maxValue)
maxValue = matrix[i2][j2];
System.out.println("Maximum numbers is " +maxValue + " at row " +row+ ", and column " +col);
double minValue =Integer.MAX_VALUE;
for (int i2 = 0; i2 < matrix.length; i2++)
for (int j2 = 0; j2 < matrix[i2].length; j2++)
if (matrix[i2][j2] < minValue)
minValue = matrix[i2][j2];
System.out.println("Minimum numbers is " +minValue+ " at row " +row+ ", and column " +col);
}
}
非常感谢你的工作 –
没有probs @ShivPatel –