How can I fix my Java program to give accurate change?

  • Comp Sci
  • Thread starter willie9142002
  • Start date
  • Tags
    Code Java
In summary, the program will give you the change for whatever random number that it picks, but it has problems with the pennies. If you need more than one penny, the program will keep repeating the output until it gets the pennines right.
  • #1
willie9142002
2
0
I had to write a java program that will give you the change for whatever random number that program picks. I keep having a problem with the pennies. If have to have more than one penny then program will repeat the output until it gets the pennines right. PLEASE HELP! I am not really good at Java so the program my look really bad please help!

import javax.swing.*;
public class CoinCounter
{

public static void main(String[] args)
{
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
int numQuarters = 0;
int numDimes = 0;
int numNickels = 0;
int numPennies = 0;



int r = (int)(Math.random() * 99);
int changeRequired = r;

while(changeRequired > 0){

if(changeRequired - quarter>= 0){
numQuarters ++;
changeRequired -= quarter;
continue;
}
if(changeRequired - dime >= 0){
numDimes ++;
changeRequired -= dime;
continue;
}
if(changeRequired - nickel >= 0){
numNickels ++;
changeRequired -= nickel;
continue;
}
if(changeRequired - penny >= 0){
numPennies ++;
changeRequired -= penny;

System.out.println("Your change is: " + r );
System.out.println(numQuarters+" Quarters");
System.out.println(numDimes+" Dimes");
System.out.println(numNickels+" Nickels");
System.out.println(numPennies+" Pennies");
}
}
}
}

Here is the output:
Your change is: 43
1 Quarters
1 Dimes
1 Nickels
1 Pennies
Your change is: 43
1 Quarters
1 Dimes
1 Nickels
2 Pennies
Your change is: 43
1 Quarters
1 Dimes
1 Nickels
3 Pennies

Process completed.
 
Physics news on Phys.org
  • #2
The pennies is the "catch all" condition. I suspect if you used larger change amounts that your program would produce unexpected results. For example, 62 cents will return most likely something other than 2 quarters, 1 dime and 2 pennies (probably 1 quarter, 2 dimes, 3 nickels and 2 pennies). Using your logic, what you SHOULD do is keep subtracting quarters until you can subtract no more, then dimes, then nickels and the remainder must be pennies finally. Do you know about modular arithmetic? You can seriously do the entire program in 1 line of code with it.
 
Last edited:
  • #3
jhicks
even with higher amounts is does that same thing only if i need more than one penny. So if it is was 62 it would give me 2 quarters, 1 dime, 1 penny then 2 quarters, 1 dime, 2 pennies. See my problem? How do I keep subtracting quarters until there are no more than dime and so on??!? I am really new and I am struggling to do this right. I have no clue what modular arithmetic is I am a 28 year old college freshman sationed overseas trying to get a degree so any help, books, or websites I should read would be a great help thanks in advance
 

1. How do I fix a syntax error in my Java code?

One common cause of a syntax error in Java code is a missing semicolon at the end of a statement. Check your code for any missing semicolons and add them in where necessary.

2. What does "null pointer exception" mean in Java?

A null pointer exception in Java means that your code is trying to access an object that does not exist or has not been initialized. Make sure all your objects are properly declared and initialized before using them.

3. How do I debug my Java code?

The best way to debug Java code is to use a debugger tool, such as Eclipse or IntelliJ, which allows you to step through your code line by line and see the values of your variables at each step.

4. What is the best way to approach solving a Java code problem?

The most effective way to solve a Java code problem is to break it down into smaller, manageable tasks. Start by identifying the main objective of your code and then work through each step, testing and debugging as you go.

5. How can I improve my Java coding skills?

The best way to improve your Java coding skills is to practice regularly and continuously challenge yourself with new projects and problems. You can also read books, attend workshops or online courses, and seek mentorship from experienced Java developers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
750
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top