我被要求打印二维数组的total,average,highestInRow,lowestInRow,rowTotal,colTotal。我有一些错误表明。
他们希望输出是这样的。
处理iarray。
总计:45
平均值:5.0
第0行:12
第1行:14行
第2行:19行
总共0:14
总共1:10
总共2:21
0:9行中最高
第1行:7行中最高
第2行:8的最高
第0行最低:1
第1行:3行最低
第2行:5最低
这是我的代码
public class Int2DArray
{
private int[][] array = { { 2,1,9},{7,3,4},{5,6,8} };
public Int2DArray(int[][] newArray )
{
for ( int i = 0 ; i < array.length ; i++ )
{
array[i][i] = newArray[i][i];
}
}
public int getTotal( int total)
{
total = 0 ;
for( int row = 0 ; row < array.length ; row++ )
{
for ( int col = 0 ; col < array[row].length ; col ++ )
total+= array[row][col];
}
return total;
}
public double getAverage ( double average )
{
int total = 0 ;
average = getTotal(total)/array.length;
return average;
}
public int getRowTotal ( int rowTotal )
{
for ( int row = 0 ; row < array.length ; row ++ )
{
for ( int col = 0 ; col < array[row].length ; col ++ )
rowTotal += array[row][col];
}
return rowTotal;
}
public int getColumnTotal ( int colTotal )
{
for ( int col = 0 ; col < array[0].length ; col ++ )
{
for ( int row = 0 ; row < array.length ; row ++ )
colTotal+= array[row][col];
}
return colTotal;
}
public void getHighestInRow ()
{
int max = 0 ;
for ( int i = 0; i < array.length; i++ )
{
max = Integer.MIN_VALUE;
for ( int j = 0; j < array [ i ].length; j++ )
if ( array [ i ] [ j ] > max )
max = array [ i ] [ j ];
System.out.println( "Maximum of row " + i + " = " + max );
}
}
public void getLowestInRow ()
{
int min = 0 ;
for ( int i = 0; i < array.length; i++ )
{
min = Integer.MAX_VALUE;
for ( int j = 0; j < array [ i ].length; j++ )
if ( array [ i ] [ j ] < min )
min = array [ i ] [ j ];
System.out.println( "Minimum of row " + i + " = " + min );
}
}
public static void main(String[]Args)
{
int total = 0 ;
double average = 0 ;
int rowTotal = 0 ;
int colTotal = 0 ;
Int2DArray object1 = new Int2DArray () ;
System.out.println(" The Total of the Array is : " + object1.getTotal(total));
System.out.println(" The Average of the Array is : " + object1.getAverage(average));
System.out.println(" The Row Total of the Array is : " + object1.getRowTotal(rowTotal));
System.out.println(" The Column Total of the Array is : " + object1.getColumnTotal(colTotal));
System.out.println(" The Highest variable in a row of the Array is : " + object1.getHighestInRow());
System.out.println(" The Lowest variable in a row of the Array is : " + object1.getLowestInRow());
}
}
答案 0 :(得分:0)
由于声明构造函数接收参数int[][]
,因此在初始化Int2DArray
对象时,必须传递一个数组。如果您不想这样做,可以定义一个没有参数的构造函数:
public Int2DArray() { }
另一个问题是:
System.out.println(" The Highest variable in a row of the Array is : " + object1.getHighestInRow());
System.out.println(" The Lowest variable in a row of the Array is : " + object1.getLowestInRow());
由于方法getHighestInRow()
和getLowestInRow()
不返回任何内容(void
)并自行打印结果,因此您必须将其称为:
System.out.println(" The Highest variable in a row of the Array is : ");
object1.getHighestInRow();
System.out.println(" The Lowest variable in a row of the Array is : ");
object1.getLowestInRow();
答案 1 :(得分:0)
这个构造函数毫无意义:
public Int2DArray(int[][] newArray )
{
for ( int i = 0 ; i < array.length ; i++ )
{
array[i][i] = newArray[i][i];
}
}
它正在多次初始化你的数组[] [],这本身没有任何意义,但你也已经用这段代码初始化了数组:
private int[][] array = { { 2,1,9},{7,3,4},{5,6,8} };
因此将其更改为
public Int2DArray()
{
}
这将是向前迈出的一大步。
旁注:这相当于简单地删除构造函数,因为空构造函数在Java中是隐式的。