gpt4 book ai didi

java - 嵌套 for 循环和变量的放置

转载 作者:行者123 更新时间:2023-12-01 11:49:31 25 4
gpt4 key购买 nike

我目前正在尝试解决一个问题,即尝试找到三角形数组中项的最小三角形和。 (我当前使用的数组是一个3x3的三角形数组)

  colAmt =1;
int tempCol = rows;
int tempSum=0;
rows = 3;

for(int t=0;t<rows;t++)
{

for(int col = 0; col<colAmt; col++)
{
tempCol=0;
tempSum =0;

for(int m=t;m<rows;m++)
{
System.out.println(m+", "+tempCol);
tempSum = tempSum + sumTriangle[m][tempCol];

tempCol++;
if(tempSum<triSum)
{
triSum = tempSum;
}
}
}
colAmt++;
}

当我执行程序时,程序打印出:

(0, 0)
(1, 1)
(2, 2)
(1, 0)
(2, 1)
(1, 0)
(2, 1)
(2, 0)
(2, 0)
(2, 0)

何时应该打印:

(0, 0)
(1, 1)
(2, 2)
(1, 0)
(2, 1)
(1, 1)
(2, 2)
(2, 0)
(2, 1)
(2, 2)

我很确定这是我处理 tempCol 变量的方式有问题,但我不知道如何修复它。任何帮助表示赞赏。谢谢!

最佳答案

替换即可得到你想要的数字

tempCol=0;

tempCol=col;

关于java - 嵌套 for 循环和变量的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28909387/

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