public static void displaySummary(int totalShipment,
String[] nameOfBus,
int[][] storageBox)
{
int counter;
//Display the Summary Report
System.out.println("\nSummary\n- - - - - - - - - - - -");
for(counter = 0; counter < totalShipment; ++counter)
{
System.out.println("Shipment #" + (counter + 1)+ " - " + nameOfBus[counter]);
System.out.print("\nXL - " + storageBox[0][counter] + ",");
System.out.print("L - " + storageBox[1][counter] + ",");
System.out.print("M - " + storageBox[2][counter] + ",");
System.out.print("S - " + storageBox[3][counter]+"\n");
}
System.out.println("\nTotal Number of containers required for these shipments:\n");
System.out.println("XL - " + totalXL);
System.out.println("L - " + totalL);
System.out.println("M - " + totalM);
System.out.println("S - " + totalS);
}
当我从main调用displaySummary时,无论是否有任何数量的货件,只会打印循环最后一圈的值...如果只有一个货件,则会打印这些值。如果两次发货,第二圈的价值会被打印出来,但第一次没有......
答案 0 :(得分:1)
这是典型的初学者错误。
如果您按以下方式添加多个项目:
int n = 0;
int[] box = new int[4];
box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;
box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;
box[0] = ...; box[1] = ...; ...
storageBox[n] = box;
++n;
错误是,您创建的同一个对象new int[]
只放置在storageBox[0], [1] and [2]
中。您将sama数组box[0..4]
多次覆盖到最后一个值。
所以storageBox[0] == storageBox[2]
及其数组值相同。
对于每个storageBox
项,您必须添加new int[4]
。
答案 1 :(得分:0)
我在下面主要的3总线上调用 displaySummary 。我认为它工作正常。那么你能告诉我们更多关于调用代码或执行平台的事情,以便我们重现bug
Summary
- - - - - - - - - - - -
Shipment #1 - Bus1
XL = 1, L = 1, M = 1, S = 1
Shipment #2 - Bus2
XL = 2, L = 2, M = 2, S = 2
Shipment #3 - Bus3
XL = 2, L = 1, M = 2, S = 1
运行:
Math.ceil(3.4456)
编辑:为了方便起见,我只修改了计数器增量(couner ++而不是++ counter)和报告格式。