Introduction to Java Programming编程题8.13<返回二维数组中最大值及下标>

来源:互联网 发布:平台介绍淘宝代理开店 编辑:程序博客网 时间:2024/06/05 20:10
/*Enter the number of rows and columns of the array: 3 4Enter the array: 11 33.5 88.1 40 -2.2 -10.1 -201.11 2.22 3.3 4.4The location of the largest element is 88.1 at (0, 2) */import java.util.Scanner;public class FindMaxValue {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("Enter the number of rows and columns of the array: ");        final int ROW = input.nextInt();        final int COLUMN = input.nextInt();        double[][] matrix = new double[ROW][COLUMN];        System.out.println("Enter the array: ");        for (int i = 0, j; i < matrix.length; i++)            for (j = 0; j < matrix[i].length; j++)                matrix[i][j] = input.nextDouble();        int[] max = findMax(matrix);        System.out.println("The location of the largest element is " + matrix[max[0]][max[1]] + " at (" + max[0] + ", " + max[1] + ")");    }    public static int[] findMax(double[][] matrix) {        int[] max = new int[2];        int k = 0;        double maxValue = matrix[k][k];        for (int i = 0, j; i < matrix.length; i++) {            for (j = 0; j < matrix[i].length - 1; j++)                if (maxValue < matrix[i][j + 1]) {                    max[k] = i;                    max[k + 1] = j + 1;            }        }        return max;    }}
0 0
原创粉丝点击