gpt4 book ai didi

java - 获取循环以添加数组的所有列

转载 作者:行者123 更新时间:2023-12-04 03:42:03 25 4
gpt4 key购买 nike

我正在尝试采用硬编码数组并打印出每一列的所有总和。每次我尝试它只是在结束之前一遍又一遍地打印第一列总和。我不知道还要添加什么才能使循环遍历整个数组。

代码如下:

public class ClimateChange {
public static void main(String[] args) {
final int ROWS = 9;
final int COLUMNS = 12;

int[][] displaced = {
{106, 107, 111, 133, 221, 767, 866, 1001, 172, 307, 392, 395},
{20, 73, 26, 82, 502, 615, 209, 947, 116, 214, 278, 445},
{163, 203, 276, 308, 172, 246, 354, 118, 123, 310, 146, 152},
{121, 260, 234, 108, 149, 202, 216, 58, 567, 229, 628, 765},
{1203, 1274, 1226, 1882, 1072, 1007, 1192, 1395, 123, 310, 146, 152},
{116, 324, 438, 714, 167, 521, 209, 904, 76, 29, 31, 99},
{76, 29, 31, 99, 187, 201, 278, 306, 183, 122, 99, 246},
{109, 104, 121, 13, 121, 69, 246, 100, 123, 161, 69, 246},
{402, 415, 209, 547, 106, 234, 178, 145, 103, 121, 39, 246}};

String[] animals = {
"Cheetah",
"Tigers",
"Asian elephant",
"Vaquita porpoise",
"Mountain gorilla",
"Red tuna",
"Orangutan",
"Black Rhinos",
"Dolphins"};

System.out.println(" Temp 0C 1C 3C " +
"5C 7C 9C 28C 32C 36C 38C 42C 45C");

System.out.println();

for (int i = 0; i < ROWS; i++) {
System.out.printf("%20s", animals[i]);
for (int j = 0; j < COLUMNS; j++) {
System.out.printf("%8d", displaced[i][j]);
}
System.out.println(); // A new line begins at the end of the row.
}

int row = 0;
int col;
int colSum = 0;

col = 0;
for (row = 0; row < displaced.length; row++)
colSum = colSum + displaced[row][col];
for (col = 0; col < displaced.length; col++) {
System.out.println("Animals: " + colSum);
}
System.out.println(" Save our animals, climate change is real!");
}
}

每次我尝试使用它时,它只会在打印数组下方一遍又一遍地打印第一列总和。

最佳答案

解决方案

你很接近,你必须在 innerLoop 中构建总和,然后在外循环中打印出总和。另请注意,您必须在外循环中再次将其重置为零

for (row = 0; row < displaced.length; row++) {
colSum = 0;

for (col = 0; col < displaced.length; col++) {
colSum = colSum + displaced[row][col];

}
System.out.println("Animals: " + colSum);
colSum = 0;
}

代码

public class Q {

public static void main(String[] args) {
final int ROWS = 9;
final int COLUMNS = 12;

int[][] displaced = { { 106, 107, 111, 133, 221, 767, 866, 1001, 172, 307, 392, 395 },
{ 20, 73, 26, 82, 502, 615, 209, 947, 116, 214, 278, 445 },
{ 163, 203, 276, 308, 172, 246, 354, 118, 123, 310, 146, 152 },
{ 121, 260, 234, 108, 149, 202, 216, 58, 567, 229, 628, 765 },
{ 1203, 1274, 1226, 1882, 1072, 1007, 1192, 1395, 123, 310, 146, 152 },
{ 116, 324, 438, 714, 167, 521, 209, 904, 76, 29, 31, 99 },
{ 76, 29, 31, 99, 187, 201, 278, 306, 183, 122, 99, 246 },
{ 109, 104, 121, 13, 121, 69, 246, 100, 123, 161, 69, 246 },
{ 402, 415, 209, 547, 106, 234, 178, 145, 103, 121, 39, 246 } };

String[] animals = { "Cheetah", "Tigers", "Asian elephant", "Vaquita porpoise", "Mountain gorilla", "Red tuna",
"Orangutan", "Black Rhinos", "Dolphins" };

System.out.println(
" Temp 0C 1C 3C 5C 7C 9C 2 32C 36C 38C 42C 45C");

System.out.println();

for (int i = 0; i < ROWS; i++) {

System.out.printf("%20s", animals[i]);
for (int j = 0; j < COLUMNS; j++) {
System.out.printf("%8d", displaced[i][j]);
}

System.out.println(); // A new line begins at the end of the row.
}

int row = 0;
int col = 0;
;
int colSum = 0;

for (row = 0; row < displaced.length; row++) {

for (col = 0; col < displaced.length; col++) {
colSum += displaced[row][col];
}

System.out.println("Animals: " + colSum);
colSum = 0;
}
System.out.println(" Save our animals, climate change is real!");

}
}

输出

              Temp        0C      1C      3C      5C      7C       9C    2  32C     36C      38C     42C     45C

Cheetah 106 107 111 133 221 767 866 1001 172 307 392 395
Tigers 20 73 26 82 502 615 209 947 116 214 278 445
Asian elephant 163 203 276 308 172 246 354 118 123 310 146 152
Vaquita porpoise 121 260 234 108 149 202 216 58 567 229 628 765
Mountain gorilla 1203 1274 1226 1882 1072 1007 1192 1395 123 310 146 152
Red tuna 116 324 438 714 167 521 209 904 76 29 31 99
Orangutan 76 29 31 99 187 201 278 306 183 122 99 246
Black Rhinos 109 104 121 13 121 69 246 100 123 161 69 246
Dolphins 402 415 209 547 106 234 178 145 103 121 39 246
Animals: 3484
Animals: 2590
Animals: 1963
Animals: 1915
Animals: 10374
Animals: 3469
Animals: 1390
Animals: 1006
Animals: 2339
Save our animals, climate change is real!

关于java - 获取循环以添加数组的所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65862581/

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