我對Java和StackOverflow很新穎(所以我希望這個問題和答案不在此處發佈)。我正在爲自動售貨機編寫代碼,我需要創建一個循環,以便它繼續詢問用戶輸入,直到達到物品成本。我將如何去做這件事?如何循環用戶輸入,直到達到總和?
System.out.println("Please enter payment of: $" + totalCost); // asking client to pay amount
double pay1; // this is the name given to the first value of money the client enters
pay1 = keyboard.nextInt();
if (pay1 > totalCost) {
System.out.println("You entered: $" + pay1);
System.out.println("Change needed: $" + (pay1 - totalCost));
} else {
System.out.println("You entered: $" + pay1);
System.out.println("Please enter payment of: $" + (totalCost - pay1));
}
double pay2; // next value of money input by customer
pay2 = keyboard.nextInt();
double totalPaid = (pay1 + pay2);
if (totalPaid > totalCost) {
System.out.println("You entered: $" + pay2);
System.out.println("Change needed: $" + (totalPaid - totalCost));
} else {
System.out.println("You entered: $" + pay2);
System.out.println("Please enter payment of: $" + (totalCost - totalPaid));
}
}
}
請問您可以展示您到目前爲止所嘗試的內容嗎? – 2015-04-01 02:19:33
我正在投票結束這個題目,因爲該題目沒有顯示任何努力,只是尋找一塊蛋糕,以減少圍巾 – 2015-04-01 02:22:52
對不起,請參閱最新的編輯 - 我希望這是你正在尋找。 – newAtThis 2015-04-01 03:39:19