java - How to loop user input until a sum is reached? -
i new java , stackoverflow (so hope question , answer aren't posted on here). writing code vending machine , need create loop continues ask user input until cost of item reached. how go this?
system.out.println("please enter payment of: $" + totalcost); // asking client pay amount double pay1; // name given first value of money 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 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)); } } }
can more specific? pseudo code this:
while(money < desiredmoney) { askformoney(); }
Comments
Post a Comment