gpt4 book ai didi

java - 幻方 其他对角线

转载 作者:行者123 更新时间:2023-12-02 05:00:20 25 4
gpt4 key购买 nike

我的其他对角线方法无法正常工作。

public int sumOtherDiag()
{
int otherDiag = 0;
for (int i = 0; i < square.length−1; i++)
{
otherDiag += square[i][i];
}
return otherDiag;
}

我没有可以在此处显示的输出,但是有人立即发现有什么问题吗?

此方法应该将元素相加并获得幻方第二条对角线(从右下开始)的总和。例如,如果我的方 block 是

01 04 03

03 05 04

05 02 04

它将输出

03 + 05 + 05 = 并得到 13

但是我的实际输出打印的数字也比预期的要少。

(没有我的输出很难解释。稍后当我访问我的程序时我会上传它)

任何帮助将不胜感激,谢谢!

最佳答案

public int sumOtherDiag()
{
int otherDiag = 0;
int count = square.length;
for (int i = 0; i < square.length; i++)
{
otherDiag += square[i][--count];
}
return otherDiag;
}

关于java - 幻方 其他对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28335311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com