public static void main(String[] args) {
String[] items = { "Ham", "Ranch", "Plantains", "Soda", "Spaghetti" };
double[] prices = { 1.99, 2.99, 3.99, 4.99, 5.99 };
int[] inventory = { 100, 200, 300, 400, 500 };
System.out.printf("We have these items available: Ham, Ranch, Plantains, Soda, Spaghetti");
System.out.printf("\nSelect an Item ->");
Scanner input = new Scanner(System.in);
String item = input.nextLine();
for (int i = 0; i < items.length; i++) {
if (items[i].equals(item)) {
System.out.printf("\nYes, we have %s. Price:%s Inventory:%s", items[i], prices[i], inventory[i]);
System.out.print("\nHow many would you like to purchase? -->");
int quantity = input.nextInt();
if (inventory[i] >= quantity) {
double total = quantity * prices[i];
System.out.printf("\nThank you for your purchase of: Item: %s \nYour total bill is: %2.2f",
items[i], total);
}else {
System.out.printf("\nSorry, we only have Inventory:%s of Item: %s", inventory[i], items[i]);
}
}else {
System.out.printf("\nSorry, we don't have %s", item);
}
}
}
}
所以,最后一个 else 语句打印了 5 次而不是一次,我不知道该怎么做才能修复它。是否在右括号之间?
元芳怎么了
相关分类