Results 1 to 6 of 6
 09042013, 11:29 PM #1Member
 Join Date
 Mar 2013
 Posts
 21
 Rep Power
 0
Problem with creating a program that calculates money
My program is supposed to ask the user to enter a dollar value from up to 100 dollars, and output the amount of bills and coins required to create that amount with the least change possible. I'm not completely finished, however when I test run the program and I enter values in the 60s and 90s, it doesnt seem to give me any info past the 50 dollar bill. Why is this happening? Thanks
XML Code:import java.io.*; public class ChangeProgram { public static void main (String [] args) throws IOException { BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); String inputA; double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0; System.out.println("Please enter a dollar amount: "); inputA = myInput.readLine(); input = Double.parseDouble(inputA); if (input <= 100 && input >= 50){ double answer = input / 50; double fifty = Math.floor(answer); System.out.println(fifty + " $50 bill(s)"); remainder = input  50; if(remainder >= 20 && remainder < 50){ double answerB = remainder / 20; double remainderA = Math.floor(answerB); System.out.println(remainderA + " $20 bill(s)"); double twenty = remainderA * 20; remainderB = remainder  twenty; if(remainderB >= 10 && remainderB < 20){ double answerC = remainderB / 10; double remainderC = Math.floor(answerC); System.out.println(remainderC + " $10 bill(s)"); double ten = remainderC * 10; remainderD = remainderB  ten; if(remainderD >= 5 && remainderD < 10){ double answerD = remainderD / 5; double remainderE = Math.floor(answerD); System.out.println(remainderE + " $5 bill(s)"); double five = remainderE * 5; remainderF = remainderD  five; if(remainderF >= 2 && remainderF < 5){ double answerE = remainderF / 2; double remainderG = Math.floor(answerE); System.out.println(remainderG + " toonie(s)"); double two = remainderG * 2; remainderH = remainderF  two; if(remainderH >= 1 && remainderH < 2){ double answerF = remainderH / 1; double remainderI = Math.floor(answerF); System.out.println(remainderI + " loonie(s)"); double one = remainderI * 1; remainderJ = remainderH  one; System.out.println(remainderJ); if(remainderJ >= 1/4 && remainderJ < 1){ double answerG = remainderJ / 1/4; double remainderK = Math.floor(answerG); System.out.println(remainderK + " quarter(s)"); double quarter = remainderK * 1/4; remainderL = remainderJ  quarter; } } } } } } } } }
 09042013, 11:43 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,811
 Rep Power
 7
Re: Problem with creating a program that calculates money
All of your if statements are nested. That won't work. What happens if you enter 20? The first test for 50 fails so the test for 20 never gets executed.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09042013, 11:58 PM #3Member
 Join Date
 Mar 2013
 Posts
 21
 Rep Power
 0
Re: Problem with creating a program that calculates money
Hi Jim,
I removed the nested loops, however, it is still skipping over certain statements. Why is this? Thanks
XML Code:import java.io.*; public class ChangeProgram { public static void main (String [] args) throws IOException { BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); String inputA; double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0; System.out.println("Please enter a dollar amount: "); inputA = myInput.readLine(); input = Double.parseDouble(inputA); if (input >= 50 && input < 100){ double answer = input / 50; double fifty = Math.floor(answer); System.out.println(fifty + " $50 bill(s)"); remainder = input  50; } if(remainder >= 20 && remainder < 50){ double answerB = remainder / 20; double remainderA = Math.floor(answerB); System.out.println(remainderA + " $20 bill(s)"); double twenty = remainderA * 20; remainderB = remainder  twenty; } if(remainderB >= 10 && remainderB < 20){ double answerC = remainderB / 10; double remainderC = Math.floor(answerC); System.out.println(remainderC + " $10 bill(s)"); double ten = remainderC * 10; remainderD = remainderB  ten; } if(remainderD >= 5 && remainderD < 10){ double answerD = remainderD / 5; double remainderE = Math.floor(answerD); System.out.println(remainderE + " $5 bill(s)"); double five = remainderE * 5; remainderF = remainderD  five; } if(remainderF >= 2 && remainderF < 5){ double answerE = remainderF / 2; double remainderG = Math.floor(answerE); System.out.println(remainderG + " toonie(s)"); double two = remainderG * 2; remainderH = remainderF  two; } if(remainderH >= 1 && remainderH < 2){ double answerF = remainderH / 1; double remainderI = Math.floor(answerF); System.out.println(remainderI + " loonie(s)"); double one = remainderI * 1; remainderJ = remainderH  one; System.out.println(remainderJ); } } }
 09052013, 12:06 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,811
 Rep Power
 7
Re: Problem with creating a program that calculates money
Look at this section of code.
Java Code:if (remainder >= 20 && remainder < 50) { double answerB = remainder / 20; double remainderA = Math.floor(answerB); System.out.println(remainderA + " $20 bill(s)"); double twenty = remainderA * 20; remainderB = remainder  twenty; } if (remainderB >= 10 && remainderB < 20) { double answerC = remainderB / 10; double remainderC = Math.floor(answerC); System.out.println(remainderC + " $10 bill(s)"); double ten = remainderC * 10; remainderD = remainderB  ten; }
You already computed at least one $50.
It gets to the $20 and figure it needs one $20. So you have $6 left. That means the test for $10 to $19 will fail. But that is where you set the remainderD. Now when you check for $5 to $9, it will fail because remainderD is 0.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09052013, 01:07 AM #5Member
 Join Date
 Mar 2013
 Posts
 21
 Rep Power
 0
Re: Problem with creating a program that calculates money
I understand what your saying, I played around with the code a bit and got it to work with numbers 50 and above but not below. Does it have something to do with th eif statements?
XML Code:import java.io.*; public class ChangeProgram { public static void main (String [] args) throws IOException { BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); String inputA; double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0, remainderN = 0, remainderP = 0, remainderR = 0; System.out.println("Please enter a dollar amount: "); inputA = myInput.readLine(); input = Double.parseDouble(inputA); if (input >= 50  input < 100){ double answer = input / 50; double fifty = Math.floor(answer); System.out.println(fifty + " $50 bill(s)"); remainder = input  50; } if(remainder >= 20  remainder < 50){ double answerB = remainder / 20; double remainderA = Math.floor(answerB); System.out.println(remainderA + " $20 bill(s)"); double twenty = remainderA * 20; remainderB = remainder  twenty; } if(remainderB >= 10  remainderB < 20){ double answerC = remainderB / 10; double remainderC = Math.floor(answerC); System.out.println(remainderC + " $10 bill(s)"); double ten = remainderC * 10; remainderD = remainderB  ten; } if(remainderD >= 5  remainderD < 10){ double answerD = remainderD / 5; double remainderE = Math.floor(answerD); System.out.println(remainderE + " $5 bill(s)"); double five = remainderE * 5; remainderF = remainderD  five; } if(remainderF >= 2  remainderF < 5){ double answerE = remainderF / 2; double remainderG = Math.floor(answerE); System.out.println(remainderG + " toonie(s)"); double two = remainderG * 2; remainderH = remainderF  two; } if(remainderH >= 1  remainderH < 2){ double answerF = remainderH / 1; double remainderI = Math.floor(answerF); System.out.println(remainderI + " loonie(s)"); double one = remainderI * 1; remainderJ = remainderH  one; } if (remainderJ >= 0.25  remainderJ < 1){ double answerG = remainderJ / 0.25; double remainderK = Math.floor(answerG); System.out.println(remainderK + " quarter(s)"); double quarter = remainderK * 0.25; remainderL = remainderJ  quarter; } if(remainderL >= 0.10  remainderL < 0.25){ double answerH = remainderL / 0.10; double remainderM = Math.floor(answerH); System.out.println(remainderM + " dime(s)"); double dime = remainderM * 0.10; remainderN = remainderL  dime; } if(remainderN >= 0.05  remainderN < 0.10){ double answerI = remainderN / 0.10; double remainderO = Math.floor(answerI); System.out.println(remainderO + " nickel(s)"); double nickel = remainderO * 0.10; remainderP = remainderN  nickel; } if (remainderP >= 0.01  remainderP < 0.05){ double answerJ = remainderP / 0.01; double remainderQ = Math.floor(answerJ); System.out.println(remainderQ + " penny(ies)"); double penny = remainderQ * 0.01; remainderR = remainderP  penny; } } }
 09052013, 01:13 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,811
 Rep Power
 7
Re: Problem with creating a program that calculates money
You don't need separate remainders for you values. Just figure out how many of the largest denomination you can use. Then, using what is left over, do the same for the next highest denomination. When you have a count of a certain denomination which is zero, ignore it.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Writing a program that calculates midpoints based on randomly picking vertices
By codehelppl0x in forum New To JavaReplies: 0Last Post: 05162013, 07:25 PM 
Fixing the loop that calculates how many days are between two dates
By Michael305rodri in forum New To JavaReplies: 14Last Post: 11062012, 10:56 AM 
program ,deposit of an amount of money in a bank
By aou in forum New To JavaReplies: 12Last Post: 08042012, 09:35 PM 
program ,deposit of an amount of money in a bank
By aou in forum Advanced JavaReplies: 1Last Post: 08032012, 09:41 PM 
Work & Earn money from home, use rebate lower price save money $$$
By arturmoniswork in forum Reviews / AdvertisingReplies: 0Last Post: 12302008, 06:57 AM
Bookmarks