且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用格式化程序格式化和二维数组?

更新时间:2022-06-12 01:52:34


,但是当我运行代码时,它只对第一行求和,然后继续将自身与前面的总和相加51次,如下所示:

but when I run the code it sums only the first row and then keeps adding itself to the previous sum 51 times to the right side like this:

查看您的代码:

int sum =0;
for(int row=0; row < votes.length; row++)
{   
    for (int col=0; col < votes[row].length; col++)
    {
        sum = sum + votes[row][col];
    }
    fmt = new Formatter();
    fmt.format("%21d", sum);
    System.out.print(fmt);  
}

看看在哪里设置 sum 设为0。现在考虑您应该在哪里将其设置为0 ...

Look at where you're setting sum to 0. Now think about where you should be setting it to 0...