/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 11 月 10 日
* 版 本 号:v1.0
*
* 问题描述:利用Java编写了一个可以输出一个数组中最大值的位置
* 输入描述:一个数组
* 程序输出:最大值及其所在位置
*/
代码:
import java.util.Scanner;
public class Location {
private int row;
private int column;
private double MAX;
Location() {
}
public static Location locateLargest(double[][] array) {
Location location1 = new Location();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] > location1.MAX) {
location1.MAX = array[i][j];
location1.row = i;
location1.column = j;
}
}
}
return location1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
int NUMBER1, NUMBER2;
System.out
.println("Enter the number of rows and columns of the array: ");
Scanner input = new Scanner(System.in);
NUMBER1 = input.nextInt();
NUMBER2 = input.nextInt();
double[][] array = new double[NUMBER1][NUMBER2];
for (int i = 0; i < NUMBER1; ++i) {
for (int j = 0; j < NUMBER2; ++j) {
array[i][j] = input.nextDouble();
}
}
System.out.println("The location of the largest element is "
+ Location.locateLargest(array).MAX + " at ("
+ Location.locateLargest(array).row + ","
+ Location.locateLargest(array).column + ")");
}
}
总结:了解java中对类的封装和在程序中是如何执行的