Com.java
//初学
import java.util.Scanner;
public class Com{
public static void main(String[] args) {
// 9.13
System.out.print("Enter the number of rows and columns in the array:");
//定义输入
Scanner inputRow = new Scanner(System.in);
Scanner inputColumn = new Scanner(System.in);
Scanner inputValue = new Scanner(System.in);
//输入数组大小
//行和列之间要使用‘Enter’
int row = inputRow.nextInt();
int column = inputColumn.nextInt();
System.out.print("Enter the array:\n");
//建立二维数组
double[][] array = new double[row][column];
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[1].length; j++) {
array[i][j] = inputValue.nextDouble();
}
}
inputValue.close();
//调用静态方法
Location l = Location.locateLargest(array);
System.out.print("The location of the largest element is " + l.maxValue + " at ");
System.out.print("(" + l.row + "," + l.column +")" );
}
}
Location.java
public class Location {
//横、纵坐标及最大值的初始化
public int row;
public int column;
public double maxValue;
Location(){
}
public static Location locateLargest (double[][] a) {
//查找最大数值的横纵坐标,并返回坐标值
//静态方法
Location l = new Location();
l.maxValue = a[1][1]; // 初始化最大值为(1,1)处
l.row = 1;
l.column = 1;
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[1].length; j++) {
if( l.maxValue < a[i][j]) {
l.maxValue = a[i][j];
l.row = i + 1;
l.column = j + 1;
}
else
continue;
}
}
return l;
}
}
注:数组是0开始,但是坐标我还是习惯从1开始,所以对row和column分别加1。