Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: please help

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Australia
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default please help

    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.....

    please help me

     
    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);
     
    	}
    }

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: please help

    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)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    inchikakoroma (May 23rd, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Location
    Australia
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: please help

    Quote Originally Posted by aussiemcgr View Post
    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)

    thanks for your help

    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.

    thanks for your help

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: please help

    That is not more/better information. You just repeated what you said in the original post.
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    May 2013
    Location
    Australia
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: please help

    because original post was changed to provide people with more info