java - 如何接受返回的二维数组
问题描述
在下面的代码中,我正在检查输入的行和列是否相等,然后接受数组并继续计算对角线元素的总和。我知道我只能为行和列取一个变量,但这就是我们的任务所说的。
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函数返回的这个二维数组。这将帮助我理解一个星期以来我无法理解的唯一概念。
和平。
解决方案
您从未声明过acceptarray
不带参数的方法。尝试int b[][] =s.acceptarray(a);
推荐阅读
- javascript - 如何在一个 React 组件中划分业务逻辑?
- r - 使用两个具有相同日期的面板数据集进行操作
- python - Flask-SQLAlchemy:不同模块之间的一对多(NoReferencedTableError)
- php - 将 .com 的所有域重定向到 wordpress 站点上的 .org
- javascript - 有什么方法可以检查另一个域上另一个网页的内容吗?
- julia - 从边缘获取顶点数
- excel - 使用 VLOOKUP 在不同列中查找相同值并打印相邻列的数据
- arrays - 将选定的组合框索引传递给数组 VBA
- git - 在 Atom 中关闭文件不会使用“git commit -a”将 Git 更改提交到存储库?
- unicode - 柱面数据库是否有 Unicode 字符?