求和数组乘法的结果

时间:2014-04-06 20:31:28

标签: java arrays multidimensional-array

我需要总结这两个数组乘法的结果:

              Item

Store   0   1   2   3    4
 0      25  64  23  45  14
 1      12  82  19  34  63
 2      54  22  17  32  35

Item     Cost Per Item
   0           $12.00
   1           $17.95
   2           $95.00
   3           $86.50
   4           $78.00

我必须总结第一行,第二行和第三行的结果,然后单独显示它们,然后将它们相加。我用于该程序的代码如下:

import java.util.*;
public class asd
{
    public static void main(String[] args)
    {
        double items[][]= new double[3][5];
        double cost[]=new double[5];
        loadArray(items, cost);
        System.out.println("Total amount of sales for each store : ");
        computeCost(items, cost);
        printArray(items, cost);
    }
    public static void loadArray(double items[][], double cost[])
    {
        Scanner input = new Scanner(System.in);
        String s1;
        int num, x, y;
        for(x=0; x<items.length;x++)
        {
            for(y=0; y<items[x].length; y++)
            {
                System.out.println("Enter the next item of data:");
                items[x][y]=input.nextDouble();
            }
        }

        //Cost of the items:
        cost[0]=12.00;
        cost[1]=17.95;
        cost[2]=95.00;
        cost[3]=86.50;
        cost[4]=78.00;
    }
    public static void printArray(double items[][], double cost[]) 
    {
        System.out.println("Number of items Sold During Day: ");
        int row, col;
        for (row =0;  row<items.length ; row++)
        {
            for(col=0; col<items[row].length; col++)
            {
                System.out.print( items[row][col]+" ");
            }
            System.out.println();
        }
        System.out.println("Cost Per Item: ");
        int i;
        for (i =0; i < 5; i++)
        {
            System.out.println(cost[i]);
        }
    }
    public static void computeCost (double items[][], double cost[])
    {
        int row, col;
        double productArray[]=new double[3];
        for (row =0;  row<items.length ; row++)
        {
            for(col=0; col<items[row].length; col++)
              {
                if(col==0)
                {
                    productArray[row]=items[row][col]*cost[0];
                    System.out.println(productArray[row]+" TEST1 ");
                }
                else if(col==1)
                {
                    productArray[row]=items[row][col]*cost[1];
                    System.out.println(productArray[row]+" TEST2 ");
                }
                else if(col==2)
                {
                    productArray[row]=items[row][col]*cost[2];
                    System.out.println(productArray[row]+" TEST3");
                }
                else if(col==3)
                {
                    productArray[row]=items[row][col]*cost[3];
                    System.out.println(productArray[row]+" TEST4 ");
                }
                else if(col==4)
                {
                    productArray[row]=items[row][col]*cost[4];
                    System.out.println(productArray[row]+" TEST5 ");
                }           
             }
        }
    }
}

computeCost()方法是乘法发生的地方,但是我想知道如何存储乘法的值,以便我以后可以将它们加起来。

(例如,首次运行的computeCost()方法的输出是:

300.0 TEST1 
1148.8 TEST2 
2185.0 TEST3
3892.5 TEST4 
1092.0 TEST5 

我需要一种方法来存储这些值并将它们添加起来,但由于它们位于循环和if语句中,我不确定如何执行此操作。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

你其实已经差不多了。出现问题的是,您要覆盖每个新列的特定行的索引值。你应该增加值。也可以为每一行(而不是数组)使用相同的变量,因为您不以任何方式重用已保存的值。

此外,不需要像检查正在考虑的列那样创建if-else结构。您可以使用col变量的值来概括该部分。

新的computeCost()方法如下所示:

public static void computeCost (double items[][], double cost[]) {
    for (int row = 0; row < items.length; row++) {
        double rowSum = 0.0;
        for(int col = 0; col < items[row].length; col++) {
            double colProduct = productArray[row] = items[row][col] * cost[col];
            rowSum += colProduct;
            System.out.println("Row: " + row + ", Col: " + col + ": " + colProduct);
        }

        System.out.println("Total row " + row + ": " + rowSum);
    }
}
相关问题