• May 22nd, 2013, 11:59 AM
inchikakoroma
i need help with the following code below....

Basically what the program should do is take and input from the user ("in dollars and cents; Eg: \$1.04 or \$6") round up the value (if necessary) and print out the relevant coin combination.

my program works well with some values but if i enter a value with a 5 Cents it doesn't print out the correct coin combination; and also I'm having problems with rounding the value.

I'm stuck.....

Code Java:

```  import java.util.Scanner;   public class CoinCalculator {   public static void main(String[] args) { Scanner scan = new Scanner(System.in); double total; int twoDollars; int oneDollar; int fiftyCents; int twentyCents; int tenCents; int fiveCents; int totalCents;   System.out.print("Please enter an Amount:\$ "); total = scan.nextDouble();   totalCents = (int)total; totalCents = (int)(100 * total);   twoDollars = (int)((total) / 2); oneDollar = (int) ((total - (twoDollars * 2)) / 1); fiftyCents = (int)((total - (twoDollars * 2) - (oneDollar * 1)) / .50); twentyCents = (int)((total - (twoDollars * 2) - (oneDollar * 1) - (fiftyCents * .50)) / .20); tenCents = (int)((total - (twoDollars * 2) - (oneDollar * 1) - (fiftyCents * .50) - (twentyCents * .20)) / .10); fiveCents =(int) ((total-(twoDollars * 2)-(oneDollar * 1)-(fiftyCents * .50) - (twentyCents * .20) - (tenCents * .10))/.05);         System.out.println("Two Dollar(s): " + twoDollars); System.out.println("One Dollar(s): " + oneDollar); System.out.println("Fifty Cent(s): " + fiftyCents); System.out.println("Twenty Cent(s): " + twentyCents); System.out.println("Ten Cent(s): " + tenCents); System.out.println("Five Cent(s): " + fiveCents);   } }```
• May 22nd, 2013, 12:24 PM
aussiemcgr
You will need to tell us what you are getting and what you expect. "It isn't working" does not provide us with enough information.

Also, are you familiar with the mod function? Basically, mod returns the remainder after division. You can simply your logic this way.

For example, let's take the following value with your layout: \$3.85
1. Two Dollars: (int)(\$3.85 / 2) = 1.
2. double tracker = 3.85 % 2 = 1.85
3. One Dollar: (int)(tracker / 1) = 1.
4. tracker = tracker % 1 = 0.85
5. Fifty Cents = (int)(tracker / .5) = 1
6. tracker = tracker % .5 = 0.35
7. Twenty Cents = (int)(tracker / .2) = 1
8. tracker = tracker % .2 = 0.15
9. Ten Cents = (int)(tracker / .1) = 1
10. tracker = tracker % .1 = 0.05
11. Five Cents = (int)(tracker / .05) = 1
12. tracker = tracker % .05 = 0

This way you don't have to redo the math for each part over and over again, which obviously reduces the risk of a mathematical mistake.
Just my 5 cents (pun intended)
• May 22nd, 2013, 10:22 PM
inchikakoroma
Quote:

Originally Posted by aussiemcgr
You will need to tell us what you are getting and what you expect. "It isn't working" does not provide us with enough information.

Also, are you familiar with the mod function? Basically, mod returns the remainder after division. You can simply your logic this way.

For example, let's take the following value with your layout: \$3.85
1. Two Dollars: (int)(\$3.85 / 2) = 1.
2. double tracker = 3.85 % 2 = 1.85
3. One Dollar: (int)(tracker / 1) = 1.
4. tracker = tracker % 1 = 0.85
5. Fifty Cents = (int)(tracker / .5) = 1
6. tracker = tracker % .5 = 0.35
7. Twenty Cents = (int)(tracker / .2) = 1
8. tracker = tracker % .2 = 0.15
9. Ten Cents = (int)(tracker / .1) = 1
10. tracker = tracker % .1 = 0.05
11. Five Cents = (int)(tracker / .05) = 1
12. tracker = tracker % .05 = 0

This way you don't have to redo the math for each part over and over again, which obviously reduces the risk of a mathematical mistake.
Just my 5 cents (pun intended)

sorry for not giving enough information

Basically what the program should do is take and input from the user ("in dollars and cents; Eg: \$1.04 or \$6") round up the value (if necessary) and print out the relevant coin combination.

my program works well with some values but if i enter a value with a 5 Cents it doesn't print out the correct coin combination; and also I'm having problems with rounding the value.