0

I am trying to get the minimum amount of coins(quarters, dimes, nickels and pennies) needed to meet a stated amount of change(1-99). Example:

Change: 93

Quarters: 3

Dimes: 1

Nickels: 1

Pennies: 3

When I run my program, I don't get the answers that I'm supposed to. My question is how do I make the output in my program look like the solution above? Do I change the equations within the program to get the numbers I want? The only part of the program I have right is the amount of quarters needed to meet the stated amount. This program also needs to work for ANY stated amount between 1 and 99.

Here's what I have so far:

package mincoins;
import java.util.Scanner;

public class MinCoins2
{

    public static void main(String[] args)

    {
        int change = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
        try ( //creates a copy of Scanner class
                Scanner in = new Scanner(System.in))
        {
            System.out.println("Please enter amount of change(1-99)");

            change = in.nextInt();

            //loop for quarters
            while (change >= 25)
            {
                change = change - 25;
                quarters = quarters + 1;
            }
            while (change >= 10)
            {
                change = change - 10;
                dimes = dimes + 1;
            }
            while (change >= 5)
            {
                change = change - 5;
                nickels = nickels + 1;
            }
            pennies = change;
            System.out.println("Change: " + change);
            System.out.println("Quarters = " + change);
            System.out.println("Dimes = " + change);
            System.out.println("Nickels = " + change);
            System.out.println("Pennies = " + change);
        }
    }//end of main
}//end of class
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

3 Answers3

1

You have typos on the last 4 lines:

System.out.println("Change:" + change);
System.out.println("Quarters= "+ change);
System.out.println("Dimes=" + change);
System.out.println("Nickels=" + change);
System.out.println("Pennies=" + change);

Are all printing change

Also, the while loop conditions should be >= instead of >.

Lastly, to print out the change at the end, you have to copy it, since you're modifying it in your while loops.

change = in.nextInt();
int changeOut = change;
...
System.out.println("Change:" + changeOut);

Live Demo

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • So, what should I put instead of change to make the output equal to the solution? – Lily Peterson Apr 05 '16 at 16:01
  • Well... ```quarters```, ```dimes```, ```nickels```, ```pennies``` for the last 4. – Jorn Vernee Apr 05 '16 at 16:02
  • I've changed it, but I still get the same result: System.out.println("Change = " + change); System.out.println("Quarters = " + quarters); System.out.println("Dimes = " + dimes); System.out.println("Nickels = " + nickels); System.out.println("Pennies = " + pennies); – Lily Peterson Apr 05 '16 at 16:04
  • Still the same result. – Lily Peterson Apr 05 '16 at 16:09
  • @LilyPeterson I recreated the problem and fixed it on my PC and Ideone, I can't help you any further, since I can not tell what the problem is. You might have another problem, try posting the output you're getting instead, and explain why it is wrong to you. – Jorn Vernee Apr 05 '16 at 16:14
  • Please enter amount of change(1-99) 93 Change:93 3 Quarters=3 Dimes=9 Nickels=18 Pennies=93 The problem is that I don't know how to make it so that the program displays the minimum amount of coins needed to meet the stated change. For 93, quarters should be 3, dimes should be 1, nickels should be 1, and pennies should be 3. It must be a math-related issue. – Lily Peterson Apr 05 '16 at 16:19
  • Perhaps you shouldn't use System.out.println(change = change - 25). Instead of printing out, just do change = change-25. It looks like change is going back to 93 every time it exits a while loop, because as you can see 93/25 = 3, 93/10 = 9, 93/5 = 18 ... as is seen in your output. – djebeeb Apr 05 '16 at 16:24
  • @LilyPeterson That sure is strange, I'm getting the expected output with the code you gave. It might help others if you update the question with that information. – Jorn Vernee Apr 05 '16 at 16:25
  • @djebeeb That is not the problem, that line updates ```change``` and outputs the new value. I tested it too. You might be on to something though. – Jorn Vernee Apr 05 '16 at 16:27
  • Still didn't work. – Lily Peterson Apr 05 '16 at 16:28
1

Obviously the typo is the problem, needs to be:

System.out.println("Quarters = " + quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickles);
System.out.println("Pennies = " + change);

But I want to make a suggestion about how this code works W.R.T. to the loops, e.g.:

//loop for quarters
while (change > 25) {
    change = change - 25;
    quarters = quarters + 1;
}

This is easily done without a loop:

quarters = change / 25;
change = change - quarters * 25;

This is because change and 25 are ints, so / will do an integer division. It will give the largest number of quarters that fit in the change, i.e. it's rounding down.

Going one step further you can also use the % modulo operator to get the remaining change at each step:

quarters = change / 25;
change = change % 25;

dimes = change / 10;
change = change % 10;

nickels = change / 5;
change = change % 5;
weston
  • 54,145
  • 21
  • 145
  • 203
  • Still doesn't work. Also, I need this program to work for ANY input between 1 and 99. – Lily Peterson Apr 05 '16 at 17:02
  • OK. You do have to fix the typo, where you are printing `change` over and over. As detailed in other answers, but I have now explicitly put that fix in my answer. – weston Apr 05 '16 at 17:09
  • Also check it is reading the entered value correctly with `System.out.println("Change: " + change);` straight after `change = in.nextInt();` – weston Apr 05 '16 at 17:10
0

The main problem is the variables you're using. You should be using your quarters, dimes, nickels, and pennies variables in the appropriate spots.

There's another logical error - you need to exhaust all of the leftover change to comprise your pennies count. Since your output (the one you actually require) doesn't mandate that you count the change left over (since it should be 0), you can avoid another loop altogether and just directly assign pennies to change:

pennies = change;

...and this will give you the right result for the total number of pennies left over.

As some general cleanup, you would also want to avoid assigning in the print statement, as it can be a bit confusing. Use statements such as change -= 25 independent of the System.out.println instead.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • It still doesn't work, no matter what I put in. – Lily Peterson Apr 05 '16 at 16:39
  • @LilyPeterson: I'm fairly confident it does; I've run the code with the prescribed changes above and I receive the exact output that you're looking for. If you're still running into trouble, you should look to add *significant detail* to your question so that we can guide you. – Makoto Apr 05 '16 at 16:40