版权声明:就是码字也不容易啊 https://blog.csdn.net/qq_40946921/article/details/84447845
设计一个名为Location的类,定义二维数组中的最大值及其位置,这个类包括公共数据域row,column和maxValue,二维数组中最大值及其下标用int型的row,colum以及double型的maxValue存储。
编写下面的方法,返回一个二维数组中最大值的位置。
public static Location locateLargest(double[][] a)
返回值是一个Location的实例,编写一个测试程序,提示用户输入一个二维数组,然后显示这个数组的最大值的位置,下面是一个运行示例。
import java.util.Scanner;
public class Location {
public int row,column;
public double maxValue;
public static Location locateLargest(double[][] a){
Location my=new Location();
my.maxValue=a[0][0];
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
if(a[i][j]>my.maxValue){
my.maxValue=a[i][j];
my.row=i;
my.column=j;
}
}
}
return my;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the number of rows and columns in the array: ");
int r=input.nextInt(),c=input.nextInt();
System.out.println("Enter the array: ");
double array[][]=new double[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
array[i][j]=input.nextDouble();
Location my=Location.locateLargest(array);
System.out.print("The location of the largest element is "+my.maxValue+" at ("+my.row+","+my.column+")");
}
}