首页 > 解决方案 > 如何接受返回的二维数组

问题描述

在下面的代码中,我正在检查输入的行和列是否相等,然后接受数组并继续计算对角线元素的总和。我知道我只能为行和列取一个变量,但这就是我们的任务所说的。

import java.util.*;

class main
{
int[][] acceptarray(int a[][])   //Accepting the 2d array
{
    Scanner xd = new Scanner(System.in);
    System.out.println ("Enter the elements of array : ");

    for( int i =0; i <a.length; i++)
    {
        for(int j=0; j<a[0].length; j++)
        {
        System.out.println("Enter element "+(i)+","+(j));
        a[i][j] = xd.nextInt();
        }
    }
    xd.close();
    return(a);
}

boolean check(int b[][])  //checking whether rows and colums are equal or not
{
    if(b.length==b[0].length)
    {return(true);}

    else
    return(false);

}

int sum(int c[][])  // Sum of the the diagonal of the matrix
{   int sum=0;
    for(int i=0;i<=c.length;i++)
    {
        for(int j=0;j<=c[0].length;j++)
        {
            if(i==j)
            {
            
            sum =sum+c[i][j];
            }
        }
    }
    return(sum);
}}

class dsum
{
public static void main(String Args[])
{
    
    Scanner xd = new Scanner(System.in);
    System.out.println("Enter number of Rows");
    int m = xd.nextInt();
    System.out.println("Enter number of Columns");
    int n = xd.nextInt();
    xd.close();
    int a[][] = new int [m][n];

    main s = new main();
    boolean p = s.check(a);
    
    if(p)
    {   
        System.out.print("It is a Square Matrix:\n\n");
        s.acceptarray(a);
        int b[][] =s.acceptarray();    //line with the error message
        s.sum(b);
        
    }
    else
    {
        System.out.println("It's not a square matrix");
        System.exit(0);
    }   

}
}

错误信息是:

                b =s.acceptarray();
                    ^
  required: int[][]
  found: no argument
  reason: actual and formal argument lists differ in length

为了记录,我是一个新手,所以我不知道所有的概念。那么,有人可以帮助我了解如何接受acceptarray函数返回的这个二维数组。这将帮助我理解一个星期以来我无法理解的唯一概念。

和平。

标签: javaarrays

解决方案


您从未声明过acceptarray不带参数的方法。尝试int b[][] =s.acceptarray(a);


推荐阅读