0
有沒有人看到錯誤的代碼?只要條件不滿意,我希望看到do-while一直在不斷工作,但肯定不是。請告訴我哪部分代碼會毀掉我的預期結果?do-while循環不會持續工作
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
ArrayList<Item> cart = new ArrayList<Item>();
Item item;
String itemName;
double itemPrice;
int quantity;
String keepShopping;
double total = 0.0;
DecimalFormat m = new DecimalFormat("######,##");
do {
System.out.print ("Enter the name of the item: ");
itemName = scan.nextLine();
System.out.print ("Enter the unit price: ");
itemPrice = scan.nextDouble();
System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();
item = new Item(itemName,itemPrice,quantity);
cart.add(item);
total +=itemPrice*quantity;
System.out.println ("Continue shopping (y/n)? ");
keepShopping = scan.nextLine();
} while (keepShopping.equals("y"));
for (int i = 0; i < cart.size(); i++) {
System.out.println(cart.get(i).toString());
System.out.println("Total price: " + m.format(total));
}
scan.close();
}
請把它降低到[mcve]並告訴我們你正在使用什麼輸入。 –
提示:閱讀完'keepShopping'後打印出來。我懷疑這個價值不是你期望的。 –
@ user307610我猜你有多餘的空格或導致值不等於「y」的東西。嘗試在keepShopping上調用修剪。 – Phil