简体   繁体   中英

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. 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? Should I add in another for loop with a counter=0 that goes as long as counter < 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}; in the beginning of the app,

int total = 0; in the beginning of the first for loop,

total += Sales[row][column]; totals[column] += Sales[row][column]; inside second for loop,

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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