我想要做的是要求用户列出项目,并创建两个数组 - 一个用于itemName,另一个用于itemPrice。我的程序,现在只用ITEMPRICE交易和没有的我怎么能在一个组合两个阵列输出两个阵列组合的列表,像这样的指示:Java - 组合两个数组?
面包 - 1.20
牛奶 - 2.00
这是我到目前为止,两个数组,但名称数组真的不包含在任何东西。谢谢!
public class TaxClass
{
private Input newList;
/**
* Constructor for objects of class Tax
* Enter the number of items
*/
public TaxClass(int anyAmount)
{
newList = new Input(anyAmount);
}
/**
* Mutator method to add items and their cost
* Enter the sales tax percentage
*/
public void addItems(double anyTax){
double salesTax = anyTax;
newList.setArray(salesTax);
}
}
public class Input
{
private Scanner keybd;
private String[] costArray;
private String[] itemArray;
/**
* Constructor for objects of class Scanner
*/
public Input(int anyAmountofItems)
{
keybd = new Scanner(System.in);
costArray = new String[anyAmountofItems];
itemArray = new String[anyAmountofItems];
}
/**
* Mutator method to set the item names and costs
*/
public void setArray(double anyValue){
for(int index=0; index < itemArray.length; index++){
System.out.println("Enter the item name: ");
itemArray[index] = keybd.next();}
for(int indexa=0; indexa < itemArray.length; indexa++){
System.out.println(itemArray[indexa]);
double totalTax=0.0;
double total=0.0;
for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
totalTax = totalTax + (cost * anyValue);
total = total + cost;
}
System.out.println("Total tax: " + totalTax);
System.out.println("Total cost pre-tax: " + total);
System.out.println("Total cost including tax: " + (total+totalTax));
}
}
是不是有一个原因,你不能创建一个对象,例如,称为'Item'有两个字符串数据成员,其中包含特定项目的名称和价格,然后创建一个'Item'数组S' – 2011-04-28 20:26:34
用'keybd.nextDouble();'替换'Double.valueOf(keybd.next())。doubleValue();'。 – 2011-04-28 20:27:18