2015-04-01 62 views
-2

我對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)); 
    } 
    } 
    } 
+1

請問您可以展示您到目前爲止所嘗試的內容嗎? – 2015-04-01 02:19:33

+1

我正在投票結束這個題目,因爲該題目沒有顯示任何努力,只是尋找一塊蛋糕,以減少圍巾 – 2015-04-01 02:22:52

+0

對不起,請參閱最新的編輯 - 我希望這是你正在尋找。 – newAtThis 2015-04-01 03:39:19

回答

1

你能更具體嗎?僞代碼可能看起來像這樣:

while(money < desiredMoney) { 
    askForMoney(); 
} 
+0

只是想知道=是否在這裏失蹤? – 2015-04-01 02:20:17

+0

在while語句中?或者別的地方? – Harvtronix 2015-04-01 02:21:05

+0

在布爾表達式中進入while?我可能是錯的,但對我來說平等是對的關係運算符? – 2015-04-01 02:21:55

相關問題