问题及代码:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Location.java
*作 者:单昕昕
*完成日期:2015年10月21日
*版 本 号:v1.0
*问题描述:设计一个位置类。
*程序输入:二维数组。
*程序输出:数组中的最大值及其位置。
*/
import java.util.*;
import java.util.Scanner;
class Location
{
int row,column;
double maxValue;
public void setRow(int i)
{
row=i;
}
public void setColumn(int j)
{
column=j;
}
public int getRow()
{
return row;
}
public int getColumn()
{
return column;
}
}
public class Test
{
public static Location locateLargest(double [][]a)
{
Location l=new Location();
l.maxValue=a[0][0];
l.setRow(0);
l.setColumn(0);
int i,j;
for(i=0; i<a.length; ++i)
for(j=0; j<a[i].length; ++j)
if(a[i][j]>l.maxValue)
{
l.maxValue=a[i][j];
l.setRow(i);
l.setColumn(j);
}
return l;
}
public static void main(String[] args)
{
Test location1=new Test();
Location location2=new Location();
System.out.print("Enter the number of rows and columns of the array:");
Scanner input=new Scanner(System.in);
int m=input.nextInt();
int n=input.nextInt();
double [][]a=new double [m][n];
System.out.println("Enter the array:");
int i,j;
for(i=0; i<m; ++i)
for(j=0; j<n; ++j)
a[i][j]=input.nextDouble();
location2 =location1.locateLargest(a);
System.out.println("The location of the largest element is "+location2.maxValue+" at ("+location2.getRow()+","+location2.getColumn()+")");
}
}
//测试数据
/*
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
*/
运行结果:
知识点总结:
类和对象
学习心得:
因为一个地方的小Bug被坑了好久,唉真是跪了。。