【问题标题】:Java: 2d array totals not completingJava:二维数组总计未完成
【发布时间】:2016-02-20 01:14:40
【问题描述】:

这是一个学校项目。它将数组中的信息显示为管状格式,每个行和列的右侧和底部都有总计。它会计算除一行之外的所有总数。我找不到问题出在哪里。谁能帮忙?!

import java.util.Scanner; 
//program uses class Scanner

    public class Ex6_20
    {
      //method begins java application
      public static void main(String[] args)
      {
        //create Scanner to obtain imput
        Scanner input = new Scanner(System.in);

        int monthySales=0; //varible to hold mothly sales
        int productNum; //varible to hold product number
        int salesNum=0; //varible to hold saleperson number
        int totalSales=0; //varible to hold total sales
        int []totalProduct = {0,0,0,0,0};//build array to hold total product
        int []salesTotal = {0,0,0,0,0};//build array to hold total salesperson

        // Build array to hold sales information by salesperson and poduct
        int[][]sales = {{2000,1500,500,800,0},
                        {500,2200,600,1000,0},
                        {1000,2000,300,2100,0},
                        {2500,4000,400,3000,0},
                        {300,3200,500,2300,0},
                        {0,0,0,0}};

        //figure total by product
        for(int row =0; row < sales.length; row++)
        {
          for(int column = 0; column < sales[row].length; column++)
            totalProduct[column] += sales[row][column];
        }//end for

        //figure total by salesperson
        for(int column = 0; column < sales.length; column++)
        {
     for(int row = 0; row < sales[column].length; row++)
     salesTotal[row] += sales[column][row];
        }//end for

        //fill totals for product
        for(int i = 0; i < 5; i++)
        {
    sales[i][4] = totalProduct[i];
        }//end for

        //fill totals for salesperson
        for(int i = 0; i < 4; i++)
        {
    sales[5][i] = salesTotal[i];
        }//end for

     // print info from array in table format
     for(int row =0; row < sales.length; row++)
        {
          for(int column = 0; column < sales[row].length; column++)
          System.out.print(sales[row][column]+"\t");
         System.out.println();

        }//end for
      } //end main method
    } //end class

【问题讨论】:

    标签: java multidimensional-array sum


    【解决方案1】:

    有几个问题:

    如果您在“按销售人员计算总计”中交换变量名称“行”和“列”,您会注意到实际上您正在执行与“按产品计算总计”完全相同的计算,只是使用了误导性变量名称。 (您使用 column 作为行索引,反之亦然。)到目前为止,totalProduct 和 salesTotal 都包含相同的值,这是不对的。

    这是主要问题。

    此外,当您显示销售总额和产品总额时,它们实际上是交换的。我怀疑这是因为您的意思是使用 totalProduct[row] += ... 每行有一个总产品,使用 salesTotal[column] += ... 计算每列的总销售额,但也许名称只是交换了行/列索引没问题。

    我实际上建议从 sales 数组中删除额外的 sum 字段,以便所有行都具有相同的列数并使用硬编码的数组边界开始,以便更清楚地知道发生了什么,直到您得到正确的总和。然后,一旦总和正确,您就可以返回并使其更普遍地工作。否则,您可能会不断收到 ArrayIndexOutOfBounds 异常,但原因不明,这就是我怀疑您到目前为止最终采用此解决方案的方式。

    例如,您可以从像这样非常简单的东西开始,然后返回并使它变得更好、更通用,以便它可以处理不同的数组大小:

        int[][] table = { { 1, 2, 3, 4, 5 }, { 2, 3, 4, 5, 6 }, { 3, 4, 5, 6, 7 } };
    
        int[] rowSums = new int[3];
        int[] colSums = new int[5];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                rowSums[i] += table[i][j];
                colSums[j] += table[i][j];
            }
        }
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(table[i][j]);
                System.out.print('\t');
            }
            System.out.println(rowSums[i]);
        }
        for (int j = 0; j < 5; j++) {
            System.out.print(colSums[j]);
            System.out.print('\t');
        }
        System.out.println();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 2013-03-16
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多