简体   繁体   English

Java-二维数组中的加法

[英]Java - Addition Within a Two-Dimensional Array

I'm working on a program that is going to use a two-dimensional array to create a table that needs to have 5 rows and 4 columns. 我正在开发一个程序,该程序将使用二维数组来创建需要有5行和4列的表。 There should be a total of each row just to the right of the values that appear and a total of each column below. 在出现的值的右边应该有每一行的总数,而在下面的每一列的总数应该是。 Here is what it should look like: 它应该是这样的:

                Emp. 1  Emp. 2  Emp. 3  Emp. 4  Product Totals
Product 1 ---->   12      24      18      23         77
Product 2 ---->   10      8       12      19         49
Product 3 ---->   28      40      22      16         106
Product 4 ---->   4       28      49      3          84
Product 5 ---->   14      17      25      9          65
//////////////////////////////////////////////
Emp. Totals -->   68      117     126     70

The problem that I am having is that I cannot figure out how to add the rows and columns up. 我遇到的问题是我无法弄清楚如何添加行和列。 Here's the code I have so far. 这是我到目前为止的代码。

public class TotalSales {

    /**
     * B. Stephens
     * CSC-151
     * This program will summarize the total sales by salesperson and product
     */
    public static void main(String[] args) {

        // create and assign values to multidimensional array
        int [][] Sales = { {12,24,18,23}, {10,8,12,19}, {28,40,22,16}, {4,28,49,3}, {14,17,25,9} };

        // display categories
        System.out.println("                 Emp. 1  Emp. 2  Emp. 3  Emp. 4  Product Totals");

        // declare ProductCounter
        int ProductCounter = 1;

        // display array
        for (int row = 0; row < Sales.length; row++){
            System.out.print("Product " + ProductCounter + " -----> ");
            for (int column = 0; column < Sales[row].length; column++){
                System.out.printf("  %d\t", Sales[row][column]);
            }
            ProductCounter ++;
            System.out.println();
        }

        System.out.println("////////////////////////////////////////////////////////////////");
        System.out.println("Emp. Totals -->");

    } // end main

} // end class

The table that I showed at the beginning is just the general format though. 我在开始时显示的表格只是一般格式。 I need to display these results after a month, so how would I get it to run 30 times and add all of them together to display a monthly total? 我需要在一个月后显示这些结果,那么如何使它运行30次并将它们全部加在一起以显示每月总计? Should I add in another for loop with a counter=0 that goes as long as counter < 30? 我是否应该添加另一个counter,计数器= 0,只要计数器<30就可以了? If so, how would I add all the results? 如果是这样,我将如何添加所有结果? I don't need the daily total. 我不需要每日总计。 I was just using that as an example of the format I need. 我只是将其用作所需格式的示例。

Something like int[] totals = {0, 0, 0, 0}; int[] totals = {0, 0, 0, 0}; in the beginning of the app, 在应用程序的开头,

int total = 0; in the beginning of the first for loop, 在第一个for循环的开头,

total += Sales[row][column]; totals[column] += Sales[row][column]; inside second for loop, 在第二个for循环内

printf(total); just after the end of the first loop, 在第一个循环结束后

and this at the very end: 最后是这样的:

for (int column = 0; column < totals.length; column++){
    System.out.printf("  %d\t", totals[column]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM