Forums Register Login

My program won't return the correct ammount of change

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Things to watch out for when doing floating point arithmetic:
http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.

Keep Coding!

Layne
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1285 times.
Similar Threads
programming help
Variable Problem, I think.
double variable and place values
Beginner Bank machine program
help for a newbie that's out of her depth!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:02:12.