2D阵列的列和行之和 [英] Sum of columns and rows of 2D array

查看:41
本文介绍了2D阵列的列和行之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在向上和向下搜索以解决我的问题,但是似乎没有任何效果。一个特定的参考-,尤其是。但是,无论我如何实现它们,我都会收到一个OutOfBoundsError,这是我无法理解的。

I've searched up and down for the fix to my issue, but none seem to work. One particular reference-- this and this, and especially this. However, no matter how I implement them, I receive an OutOfBoundsError, which I can't understand.

该程序对于某堂课来说是额外的功劳。实际上,这非常简单-

The program is extra credit for a class. In truth, it is very simple--


程序说明:使用二维数组可以解决以下问题。一个公司有四个销售人员(1-4),他们销售五个不同的产品(1-5)。每天一次,每个销售员都会针对每种不同类型的产品传递一张纸条。每张纸条包含:

Program Description: Use a two dimensional array to solve the following problem. A company has four sales persons (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains:

销售人员编号
产品编号
当天销售的产品的总价值

The sales persons number
The product number
The total dollar value of that product sold that day

因此,每个销售员每天传递0到5个销售单。假设上个月所有票据的信息均可用。每个数据行包含3个数字(销售人员编号,产品编号,销售量)。

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Each data line contains 3 numbers (the sales person number, product number, sales).

编写一个程序,该程序将读取上个月销售的所有这些信息,并汇总销售人员的总销售额

Write a program that will read all this information for last month’s sales, and summarize the total sales by salesperson by product.

提供的数据:

1 2 121.77
1 4 253.66
1 5 184.22
1 1 97.55
2 1 152.44
2 2 104.53
2 4 189.97
2 5 247.88
3 5 235.87
3 4 301.33
3 3 122.15
3 2 301.00
3 1 97.55
4 1 125.66
4 2 315.88
4 4 200.10
4 3 231.45

仅在尝试计算列时才会出现错误。我的行工作;无论我如何更改数组的行或列中的for循环或任何indes,它都行不通。首先,我分别计算了行数,然后对列求和,这也不起作用。

The error only comes when it tries to calculate the columns. My rows work; no matter how I change the for-loop or any of the indeces in the row or column of the array, it doesn't work. I at first had my rows calculated separately, then my column sums, and it didn't work either. There is something that I'm missing that I'm clearly overlooking.

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;


public class prog480u {

    static Scanner inFile = null;

    public static void main(String[] args) {
        
        try {

            // create scanner to read file
            inFile = new Scanner(new File ("prog480u.dat"));

        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            System.exit(0);
        }
        
        // make the array
        
        int x = 0;
        int y = 0;
        
        double[][] profits = new double[4][5];
        
        while (inFile.hasNext()) {
        
            x = inFile.nextInt();   // use sales numbers as coordinates
            y = inFile.nextInt();
                                
                profits[x - 1][y - 1] = inFile.nextDouble();            
                
            }

        
        // check if it's okay

        
        System.out.println("");
        double[][] columnProfits = sums(profits);
        
        for (int a = 0; a < columnProfits.length; a++) {
            System.out.print((a+1) + "\t");
            for (int b = 0; b < columnProfits[a].length; b++) {
            System.out.print(columnProfits[a][b] + "\t");
        }
    
        System.out.println("");
        }

        double[] bottomRow = columnSums(columnProfits);
        
        for (int a = 0; a < bottomRow.length; a++) {
            
            System.out.print("Total:" + bottomRow + "\t");
            
        }

        
    }
    
    public static double[][] sums (double[][] q) {
        
        double[][] array = new double[5][6];
        array = q;

        double sum = 0;     
        
        for (int a = 0; a < array.length; a++) {
            
            for (int b = 0; b < array[0].length; b ++) {
                
                sum += array[a][b]; // add everything in the row
                
                
            }
            
            array[a][4] = sum;  // set that row to the last column
            
            sum = 0;    // reset sum to 0
            
        }
        
        return array;
    }
    
    public static double[] columnSums (double[][]q) {
        
        double[][] array = new double[5][6];
        array = q;
        
        double sum2 = 0;
        
        double[] columns = new double [5];

        for (int a = 0; a < array.length; a++) {
            
            for (int b = 0; b < array[0].length; b ++) {
                
                sum2 += array[b][a];
                
                columns[b] = sum2;
                
            }
            
            sum2 = 0;   // reset sum to 0
            
        }
        
        return columns;
    }
    
        
}
        

    
    

非常感谢您的宝贵时间。我感觉我的程序快要运行了,但是这个小错误把我逼到了极限。

Thank you very much for your time. I have a feeling my program is close to working, but this small mistake is pushing me over the edge.

推荐答案

代码(我做了一点清理):

Here's the working code (I cleaned it up a bit):

您非常接近,您只需要在for循环中交换最大索引。这就是为什么您得到 java.lang.ArrayIndexOutOfBoundsException

You were very close, you just needed to swap your max indicies in the for loops. That's why you were getting a java.lang.ArrayIndexOutOfBoundsException

public static double[] columnSums(double[][] q)
{
    double[][] array = q;

    double[] columns = new double[5];

    for (int a = 0; a < array[0].length; a++)
    {
        for (int b = 0; b < array.length; b++)
        {   
            columns[a] += array[b][a];
        }
    }
    return columns;
}

这篇关于2D阵列的列和行之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆