Results 1 to 6 of 6
  1. #1
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default help with second problematic kid

    hi again guys, i continued doing the personal projects after the earlier question. the tips that people gave me earlier seemed to be doing the job.
    this time it's a whole new problem.
    i programed a program which you input a number of different coins and it will tell you how much money you have in coins.
    When you DO put in the coin values, you get a wrong value... if you enter 0's for everything, you end up with $0.16000000001.
    If you enter 1's for everything, you end up with $4.91, when it should actually be $1.91.
    I went through the whole thing 10 times, but I just can't seem to figure it out. Help please?

    /* Write an application that determines the value of the coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels, and pennies. */
    import java.util.Scanner; // importing a scanner which will scan what the users will input.
    public class pp0208 {
    public static void main (String[] args){

    double dollars, half_dollars, quarters, dimes, nickels, pennies; // units that the user will input.

    Scanner scan = new Scanner (System.in); // using the scanner that had been imported in the beginning of the program.

    System.out.println ("Input number of dollars: "); // input number of dollar coins
    dollars = scan.nextDouble ();

    System.out.println ("Input number of half-dollars: "); // input number of half-dollars
    half_dollars = scan.nextDouble ();

    System.out.println ("Input number of quarters: "); // input number of quarters
    quarters = scan.nextDouble ();

    System.out.println ("Input number of dimes:"); // input number of dimes
    dimes = scan.nextDouble ();

    System.out.println ("Input number of nickels:"); // input number of nickels
    nickels = scan.nextDouble ();

    System.out.println ("Input number of pennies:"); // input number of pennies
    pennies = scan.nextDouble ();

    System.out.println ("The number of coins that you input are: " + dollars + " dollars, " + half_dollars + " half-dollars, " + quarters
    + " quarters, " + dimes + " dimes, " + nickels + " nickels, and " + pennies + " pennies.");
    // tells you what you input

    dollars = (dollars * 1) + (half_dollars * 0.50) + (quarters * 0.25) + (dimes + 0.10) + (nickels + 0.05) + (pennies + 0.01);

    System.out.println ("How much you have is: $" + dollars); // tells you how much you have in coins

    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How come for dimes, nickels and pennies you are adding the values?

    Try declaring a total variable and see why happens. The code looks good but I believe you should be multiplying smaller demoninations by the value as well.

    By the way, when posting code please wrap it in code blocks to maintain indentation and make it easier for us to read
    [co de]
    code here
    [/code]

    Also make the coin variables integers, you don't want someone putting in half a quarter.
    Last edited by sunde887; 04-02-2011 at 11:07 AM.

  3. #3
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    oh you are awsome!
    i guess my untrained eyes are still not seeing the small errors like this.
    I feel stupid for staring at this for an hour.
    Thank You!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You fixed it?

  5. #5
    elecleoalune is offline Member
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    yes! thanks to you! now i am working on the next program that might become the third problematic kid, but this time i am going to try to work on it myself. hope it goes well with it.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Hopefully you can figure the third one without our help, but if you cant dont hesitate to ask.

Similar Threads

  1. is this IOException problematic?
    By ozzyman in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 06:34 PM
  2. Replies: 0
    Last Post: 08-03-2009, 08:04 AM
  3. minimum word length in search problematic
    By jocassid in forum Suggestions & Feedback
    Replies: 3
    Last Post: 07-01-2008, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •