我无法弄清楚如何将二维数组的列乘以一维数组......
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
1 $12.00
2 $17.95
3 $95.00
4 $86.50
5 $78.00
我必须将符号化项目的2D数组的列乘以它们的成本(I.E.第1列2D数组乘以$ 12.00,第2列(2D数组)的价格为17.95美元。
我主要理解的是如何让程序只乘以列而不是行和列,任何帮助都会受到赞赏。
编辑:
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.99;
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[5];
for (row =0; row<items.length ; row++)
{
for(col=0; col<items[1].length; col++)
{
productArray[row]=items[row][col] * cost[0];
}
System.out.println("TEST: "+productArray[row]);
}
}
}
循环必须为第一列执行此操作:
double a,b,c;
a=items[0][0]*cost[0];
System.out.println("Test 12.99*25: "+a);
b=items[1][0]*cost[0];
System.out.println("Test 12.99*12: "+b);
c=items[2][0]*cost[0];
System.out.println("Test 12.99*54: "+c);
编辑:我完成了该计划,感谢对评论人员的帮助。