我无法弄清楚如何使方法purchaseItems(Warehouse w[])
起作用。
是否通过嵌套for循环正确传递w[]
数组的任何建议?
import javax.swing.JOptionPane;
public class WarehouseTest
{
public static void main(String args[])
{
String dataArray[][] = {{ "2200", "10", "5", "5", "First Street", "Dallas", "Texas", "76000"},
{ "5550", "12.75", "2", "3", "Mitchell Drive", "Arlington", "Texas", "76019"},
{ "12000", "17.50", "6", "7", "Jones Blvd", "Burleson", "Texas", "76120"},
{ "7235", "22.35", "54", "80", "Smith Circle", "Keller", "Texas", "75020"},
};
Warehouse wArray[] = new Warehouse[4];
wArray = createWarehouses(dataArray);
JOptionPane.showMessageDialog(null, purchaseItems(wArray));
JOptionPane.showMessageDialog(null, printWarehouses(wArray));
JOptionPane.showMessageDialog(null, printWarehouseCharge(wArray));
}
public static Warehouse[] createWarehouses( String a[][])
{
Warehouse w[] = new Warehouse[a.length];
for( i=0; i<w.length; i++)
{
w[i] = new Warehouse(Integer.parseInt(a[i][0]),
Double.parseDouble(a[i][1],
Integer.parseInt(a[i][2]),
Integer.parseInt(a[i][3]),
new Address(a[i][4]),
(a[i][5]),
(a[i][6]),
(Integer.parseInt(a[i][7]))));
}
return w;
}
public static String purchaseItems(Warehouse w[])
{
Sring msg = "";
String data[][] = {{ "Dallas", "1", "2"},
{ "Arlington", "0", "5"},
{ "Burleson", "3", "0"},
{ "Keller", "10", "25"},
{ "Dallas", "5", "0"},
{ "Arlington", "0", "1"},
{ "Burleson", "2", "4"},
{ "Keller", "0", "30"},
};
for( int i=0; i<data.length;i++)
{
for(w=0; i<w.length; w++)
{
if (w[i].getAddress().getCity.equals(data[i][0]))
{
String msg += String.format("$%.2f"\n, (w[i].purchaseTelevision(Double.parseDouble(data[i][1], 599.0) + w[i].purchaseComputer(Double.parseDouble(data[i][2], 759.0));
}
}
}
return msg;
}
public static String printWarehouses(Warehouse w[])
{
String msg = "";
for(i=0; i<w.length;i++)
{
msg += String.format("%s\n", w[i].toString());
}
return msg;
}
public static String printWarehouseCharge(Warehouse w[])
{
String msg = "";
for(i=0; i<w.length; i++)
{
msg += String.format("$%,.2f\n", w[i].calculateWarehouseCharge());
}
return msg;
}
} // end class WarehouseTest
答案 0 :(得分:1)
修改循环:
for( int i=0; i<data.length;i++)
{
for(int j=0; j<w.length; j++) // Changed the variable 'w' to 'j'
{
if (w[j].getAddress().getCity.equals(data[i][0]))
{
String msg += String.format("$%.2f"\n, (w[i].purchaseTelevision(Double.parseDouble(data[i][1], 599.0) + w[i].purchaseComputer(Double.parseDouble(data[i][2], 759.0));
}
}
}
在for循环for(w=0; i<w.length; w++)
中,你将int值赋给数组对象'w',因此它会导致问题。