gpt4 book ai didi

java - 从二维数组中查找最大总数并在 JAVA 中打印

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

所以在这里我尝试从数组中获取一些整数并将它们添加到每行中。然后我将总和存储为 rowTotal。如果 rowTotal 大于 maxRow....我累了。有人可以指出显而易见的事情吗?

     int maxRow = 0;
int playNum = 0;

for(int col = 0;col < score[0].length;col++)
maxRow += score[0][col];
for(int row = 1;row < score.length;row++)
{
int rowTotal = 0;
for(int col = 0;col < score[row].length;col++)
rowTotal += score[row][col];
if(rowTotal>maxRow)
{
playNum = row;
maxRow = rowTotal;
}
System.out.println("Player " + (playNum + 1) + " is victorious with a score of " + maxRow);
}

最佳答案

您需要将System.out.println从第二个for循环中取出。另外,您可以取消第一个 for 循环。

 int maxRow = 0;
int playNum = 0;
for(int row = 0;row < score.length;row++)
{
int rowTotal = 0;
for(int col = 0;col < score[row].length;col++)
{
rowTotal += score[row][col];
}
if(rowTotal>maxRow)
{
playNum = row;
maxRow = rowTotal;
}

}
System.out.println("Player " + (playNum + 1) + " is victorious with a score of " + maxRow);

关于java - 从二维数组中查找最大总数并在 JAVA 中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368232/

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