내가하려는 것은 사용자에게 항목 목록을 요청하고 두 개의 배열을 만듭니다. 하나는 itemName에 대한 것이고 다른 하나는 itemPrice에 대한 것입니다. 내 프로그램은 지금에만 itemPrice 다루는 나는 다음과 같이 출력 한 결합 두 배열의 목록을 두 배열을 결합 할 수있는 방법의 징후가 없습니다 :자바 - 두 배열을 결합 하시겠습니까?
빵 - 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' 배열을 만듭니다. 에스? –
'Double.valueOf (keybd.next()). doubleValue();를'keybd.nextDouble();'으로 대체하십시오. –