Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取循环以添加数组的所有列_Java_Arrays_Multidimensional Array - Fatal编程技术网

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

Java 获取循环以添加数组的所有列,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我试图获取一个硬编码数组,并打印出每列的所有和。每次我尝试它只是在结束前反复打印第一列和。我不知道还要添加什么来保持循环在整个阵列中运行 代码如下: 公共类气候变化{ 公共静态void main(字符串[]args){ 最终整数行=9; 最终整数列=12; int[][]位移={ {106, 107, 111, 133, 221, 767, 866, 1001, 172, 307, 392, 395}, {20, 73, 26, 82, 502, 615, 209, 947, 116, 214,

我试图获取一个硬编码数组,并打印出每列的所有和。每次我尝试它只是在结束前反复打印第一列和。我不知道还要添加什么来保持循环在整个阵列中运行

代码如下:

公共类气候变化{
公共静态void main(字符串[]args){
最终整数行=9;
最终整数列=12;
int[][]位移={
{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}};
字符串[]动物={
“猎豹”,
“老虎”,
“亚洲象”,
“瓦基塔鼠海豚”,
“山地大猩猩”,
“红金枪鱼”,
“猩猩”,
“黑犀牛”,
“海豚”};
系统输出打印项次(“温度0C 1C 3C”+
“5C 7C 9C 28C 32C 36C 38C 42C 45C”);
System.out.println();
对于(int i=0;i
每次我尝试使用它时,它只是在打印的数组下面一遍又一遍地打印第一列和。

解决方案 如果很接近,则必须在内部循环中构建和,然后在外部循环中打印出和。 还请注意,您必须在outerloop中将其再次重置为零

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!