加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java Magic Square – 求和列和求和行错误

发布时间:2020-12-15 02:09:19 所属栏目:Java 来源:网络整理
导读:我被分配了一个学校作业来制作并检查用户在N * N矩阵中生成的二维阵列中的“幻方”. 到目前为止,我已经获得了大部分代码(我已经单独测试了每个方法).但是,我无法纠正在我的’sumColumn’和’sumRow’方法中不断出现的两个最终错误.这是我上述两种方法的代码
我被分配了一个学校作业来制作并检查用户在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;

解决方法

sumColumn方法需要两个参数:

> int [] [] square
> int columnNumber

在你的main方法中,你似乎只提供了一个参数int [] [],而你忘了将columnNumber包含为第二个参数.

同样适用于sumRow方法.

我根据您发布的错误消息做出了上述调查结果:

MagicSquares类型中的方法sumRow(int [] [],int)不适用于squares.MagicSquares.validMagicSquare(MagicSquares.java:105)中的参数(int [] [])

它说,在您的MagicSquares.java文件中,行号105,您调用sumRow方法.它进一步表明(方法sumRow(int [] [],int),类型MagicSquares),你实现的方法,不能使用/不适用于带参数的调用(int [] []),at第105行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读