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: Need help with this program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with this program

    import java.util.Scanner;
    public class PP_2_11a
    {
    //---------------------------------------------------------------------
    // Reads amount of money and determines what bills and coins are needed
    //---------------------------------------------------------------------

    public static void main (String[]args)
    {
    double total_amount;
    Scanner scan = new Scanner (System.in);



    boolean more = true;

    while (more)
    {
    System.out.println ("Enter amount of money.");
    total_amount = scan.nextDouble();


    int tens = (int)total_amount/10;
    total_amount = total_amount%10;
    int fives = (int)total_amount/5;
    total_amount = total_amount%5;
    int ones = (int)total_amount/1;
    total_amount = total_amount%1;
    int quarters = (int)(total_amount/.25);
    total_amount = (total_amount%.25);
    int dimes = (int)(total_amount/.10);
    total_amount = (total_amount%.10);
    int nickels = (int)(total_amount/.05);
    total_amount = (total_amount%.05);
    int pennies = (int)(total_amount%.01);



    System.out.println (tens + " ten dollar bills");
    System.out.println (fives + " five dollar bills");
    System.out.println (ones + " one dollar bills");
    System.out.println (quarters + " quarters");
    System.out.println (dimes + " dimes");
    System.out.println (nickels + " nickels");
    System.out.println (pennies + " pennies");

    System.out.print ("Enter 1 to continue, 0 to quit > ");
    more = (1 == scan.nextInt());
    }


    }
    }


    it does not calculate properly when I input the amount 68.97
    please help
    thank you!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with this program

    it does not calculate properly when I input the amount 68.97
    Please explain what the code should do.
    Copy and paste here the contents of the console screen from when you run the program
    Add some comment to the output saying what the output should be.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    There are some problems working with floating point numbers.
    Read this:http://download.oracle.com/docs/cd/E..._goldberg.html

    One trick is to convert the amount to cents and scale your divisors accordingly.
    Last edited by Norm; September 18th, 2011 at 04:48 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this program

    my program needs to be able to read the amount of money and then output how many tens, fives, ones, quarters, dimes and so on that are needed to make the amount.
    When I run the program it does everything right except it does not calculate the amount of pennies right:
    ----jGRASP exec: java PP_2_11a

    Enter amount of money.
    68.97
    6 ten dollar bills
    1 five dollar bills
    3 one dollar bills
    3 quarters
    2 dimes
    0 nickels
    1 pennies
    Enter 1 to continue, 0 to quit >

    there should be 2 pennies but I do not understand why it is not calculating it right.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with this program

    Add some printlns in your code to print out the intermediate values so you can see what is happening.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javalava11 (September 18th, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this program

    I was able to find out where it went wrong when I put in the printlns-thank you!

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM