1 回答
TA贡献1798条经验 获得超7个赞
您必须自己测试该元素在循环之后是否不存在boolean。此外,如果找到,则无需继续循环(所以break)。并使用%nwithprintf获取换行符。喜欢,
boolean found = false;
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 %n"
+ "Your 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]);
}
found = true;
break;
}
}
if (!found) {
System.out.printf("%nSorry, we don't have %s", item);
}
添加回答
举报