【问题标题】:Getting a loop to add all the columns of an array获取循环以添加数组的所有列
【发布时间】:2021-01-23 17:58:11
【问题描述】:

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

代码如下:

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!");
    }
}

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

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    解决方案

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

    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!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2018-06-03
      • 2018-06-27
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多