This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Variable Problem, I think. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable Problem, I think." Watch "Variable Problem, I think." New topic

Variable Problem, I think.

Joseph Fisher

Joined: Jun 09, 2008
Posts: 5
I have this program. I'm supposed to calculate change with appropriate coins. I've been over this a bunch and my math and loops seems to be in order. It's just not coming out right. I output the change value to the screen throughout the loops and at some point it gets all wonky. I can't understand why. Can somebody point me in the direction as to why? Thanks.

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
I tried couple of cases and it worked fine.

May be you want to post the values, it didn't work?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38025
You do realise you NEVER use floating-point arithmetic for money in real life? You can get some nasty errors when you get values like 1.999999999999999999999999 instead of 2.0.

Were you told you use loops to calculate the change? I would have used the % and / operators, which of course work better using integer arithmetic. Your while(change - 1 >= 0) is an awkward formula.
I agree. Here's the link:
subject: Variable Problem, I think.
Similar Threads
Need help creating classes
double variable and place values
Vending Machine Project
nice easy question.....i think
Display nothing if == 0