我的教授非常严格,只想知道我是否正确地解决了这个问题。
设计一个名为Location的类来定位一个最大值及其在二维数组中的位置。该类包含公共数据字段行、列和maxValue,它们将最大值及其索引存储在二维数组中,行和列作为int类型,maxValue作为双重类型。编写以下方法,返回二维数组中最大元素的位置:
public static Location locateLargest(double[][] a)守则是:
public static void main(String[] args) {
System.out.println("Please enter the row and column size");
int row = kk.nextInt();
int column = kk.nextInt();
System.out.println("Enter the values between 1 and 10");
double[][] d = getArray(row,column);
Location l = locateLargest(d);
System.out.println(l.toString());
}
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
double input;
for (int i=0;i<a.length;i++){
for (int j=0;j<a[i].length;j++){
a[i][j] = kk.nextDouble();
}
}
return a;
}
public static Location locateLargest(double[][] a){
int rowIndex=0;
int columIndex=0;
double max = a[rowIndex][columIndex];
for (int i=0;i<a.length;i++){
for (int j=0;j<a[i].length;j++){
if (a[i][i]>max) {
max = a[i][j];
rowIndex=i;
columIndex=j;
}
}
}
return new Location(rowIndex,columIndex,max);
}
}
class Location{
int row,column;
double maxValue;
Location(){
}
Location(int row,int column,double maxValue){
this.row = row;
this.column = column;
this.maxValue = maxValue;
}
public String toString(){
return "The largest value is "+maxValue+" at row "+row+" and column "+column;
}
}发布于 2017-05-07 06:09:08
Location必须是public class Location,因为使用非public返回类型的public方法是没有意义的。Location类的字段必须是public int和public double,因为任务描述这样说。Location类的字段应该是public final int和public final double,因为它们在初始化一次后不会更改。Ctrl+Shift+F或IntelliJ中的Ctrl+Alt+L。https://codereview.stackexchange.com/questions/162723
复制相似问题