Java 如何接受返回的2d数组
在下面的代码中,我检查输入的行和列是否相等,然后接受数组并继续计算对角线元素的和。我知道我只能为行和列取一个变量,但这就是我们的赋值Java 如何接受返回的2d数组,java,arrays,Java,Arrays,在下面的代码中,我检查输入的行和列是否相等,然后接受数组并继续计算对角线元素的和。我知道我只能为行和列取一个变量,但这就是我们的赋值 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
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);
}
}
}
作为记录,我是一个新手,所以我不知道所有的概念。
那么,有人可以帮助我如何接受acceptarray函数返回的2d数组吗。这将帮助我理解一周以来我唯一无法理解的概念
和平。您从未声明过不接受参数的方法。尝试
intb[][]=s.acceptarray(a)
您从未声明过不接受参数的方法。尝试intb[][]=s.acceptarray(a)代码>您不需要b
。只需调用s.sum(a)
您没有将任何变量传递给acceptarray
方法。您必须根据方法签名传递参数。您不需要b
。只需调用s.sum(a)
您没有将任何变量传递给acceptarray
方法。必须根据方法签名传递参数。
b =s.acceptarray();
^
required: int[][]
found: no argument
reason: actual and formal argument lists differ in length