Question by tambourine: simple java program “bakesale”?
im trying to make a noob program that
asks how much money they have (lets say 10.33)
tells them how many they can buy with that money (50c each)
lists how much change they get back
the problem i’m having is in my version, the program tells the user they can buy fractions of brownies (like 1.42 brownies) and I don’t want it to do that.
I end up taking the total amount they have, minus what they can buy, multiplied by price and it equals zero. If someone can point me in the right direction how to make the number they can buy a whole number would be great
Best answer:
Answer by pyr0
use the % (modulus). It gives remainders of a number divided by another.
10%3=1
5%2=1
14%6=2
10.33%0.50=.33
Give your answer to this question below!
do you mean using
Math.round(userInput)
so that the user gets the closes number to what they enter or maybe
(int) userInput
so the user gets the largest integer less than the number,
or tell the user they can’t enter that?
if( userInput – (int) userInput > .001)
System.out.println(“Invalid input”);