First off I would like to say that I have only been programing for about 4 weeks so I'm sorry if this post is abysmally silly. However, the assignment that I am currently working on doesn't seem to be working correctly and I cannot figure out why. It compiles just fine and runs fine but for some reason it will not return what I designed it to return
the problem appears to be with the pennie while statement...
the program will return the correct ammount of change for several values but it often returns the incorrect number of pennies. For instance 15.88 returns 3 pennies while 15.87 returns only 1
PLZZZZZZZZZ help my project is due in like 2 hours and I thought it worked fine but now it's broken. Any help would be greatly appreciated as I have spent several hours trying to troubleshoot it myself.
[ February 09, 2005: Message edited by: russell lloyd ] [ February 09, 2005: Message edited by: russell lloyd ]
A quick fix would be to represent the money as int (number of pennies) rather than float (number of dollars) if that is possible.
Other than that, floating point arithmatic is not exact so you would want to use BigDecimal.
Joined: Feb 09, 2005
Thank both of you so much my program works now. I think it would have been nice of my teacher to point that out to us but hey she hasn't even taught us how to use while loops yet so I have spent the last 4 hours trying to learn about while loops. Both of you are eternally my heroes for you have saved my grade. THANK YOU THANK YOU THANK YOU
If you understand integer arithmetic, you can actually do this whole thing without any while loops at all. Since you've already done your own implementation, I don't feel bad at all showing you another way to do it:
Notice that the variable names I use above all start with lower case. This is a common convention. Usually class names start with upper case. I would strongly suggest that you follow this convention, especially since it will help others here at the Ranch understand your code more quickly.
Perhaps the trickiest part I use in the code above, is the -= operator. This is just a shortcut: "x -= y" is the same as "x = x - y". Also note that every variable above has type int. The division might work differently than you expect. I won't go into details, but if you'd like to run the code and figure out how it works, you'll need to learn about so-called "integer division".
Mostly, I hope this shows that there is often more than one way to tackle a problem. I don't wish to invalidate your attempts at all. Rather I'm just showing a different way of approaching the problem.