我被分配了一项学校作业,以制作和检查用户在 N*N 矩阵中生成的二维数组中的“魔方”。
到目前为止,我已经正确地编写了大部分代码(我已经单独测试了每种方法)。但是,我无法纠正在我的“sumColumn”和“sumRow”方法中不断出现的两个最终错误。这是我上述两种方法的代码:
public static int sumColumn(int[][] square, int columnNumber)
{
int sum = 0 ;
for (int j = 0; j < square.length ; j++)
{
for (int i = 0; i < square.length; i++)
{
sum = sum + square[i][j] ;
}
}
return sum ;
}
public static int sumRow(int[][] square, int rowNumber)
{
int sum = 0 ;
for (int i = 0; i < square.length; i++)
{
for (int j = 0; j < square.length; j++)
{
sum = sum + square[i][j] ;
}
}
return sum ;
}
这是输出以及从 main 方法调用时出现的错误:
Please enter a value for N:
1
Please enter 1 numbers:
1
This is the square you input:
+-+
|1|
+-+
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
The method sumColumn(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.main(MagicSquares.java:167)
对“sumRow”和“sumColumn”的一些摆弄会产生另一个错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
rowNumber cannot be resolved to a variable at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
colNumber cannot be resolved to a variable at squares.MagicSquares.main(MagicSquares.java:167)
任何帮助将不胜感激解决这个问题!谢谢!
PS:我上个月才开始编程,所以请善待:3
编辑:这里是检查每行、列以及主对角线和次对角线是否等于制作魔方的方法。
boolean status = true ;
int sum = sumDiagonal(square) ;
if (sumSecondaryDiagonal(square) != sum)
{
status = false ;
}
else
{
for (int row = 0; (row < square.length) && status; row ++)
{
if (sum != sumRow(square, square.length))
{
status = false ;
}
}
for (int col = 0; (col < square.length) && status; col ++)
{
if (sum != sumColumn(square, square.length))
{
status = false ;
}
}
}
return status;