Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 声明对象的解决方案。表示它需要int[][]_Java_Object - Fatal编程技术网

Java 声明对象的解决方案。表示它需要int[][]

Java 声明对象的解决方案。表示它需要int[][],java,object,Java,Object,我被要求打印二维数组的合计、平均、最高行、最低行、行合计、列合计。我发现了一些错误 他们希望输出是这样的 处理射线 总数: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

我被要求打印二维数组的合计、平均、最高行、最低行、行合计、列合计。我发现了一些错误

他们希望输出是这样的

处理射线

总数: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());

    }
}
公共类Int2DArray
{
私有int[][]数组={{2,1,9},{7,3,4},{5,6,8};
公共Int2DArray(int[][]newArray)
{
for(int i=0;imax)
max=数组[i][j];
System.out.println(“行的最大值”+i+“=”+max);
}
}
public void getLowestInRow()
{
int min=0;
for(int i=0;i
由于构造函数被声明为接收参数
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();

此构造函数毫无意义:

public Int2DArray(int[][] newArray )
{
    for ( int i = 0 ; i < array.length ; i++ )
    {
        array[i][i] = newArray[i][i];
    }
}
所以把它改成

public Int2DArray()
{
}
这将是向前迈出的一大步

旁注:这相当于简单地删除构造函数,因为空构造函数在Java中是隐式的。

“我有一些错误显示出来。”所以告诉我们它们是什么,并在文本中执行,而不是在代码中执行。。。然后正确格式化代码。看见
public Int2DArray()
{
}