Results 1 to 2 of 2

Thread: Inaccurate?!

Hybrid View

  1. #1

    Thread Starter
    Junior Member guitarhero14's Avatar
    Join Date
    Nov 2005
    Posts
    23

    Inaccurate?!

    I am completely confused over this. I dont know why the dimes and nickels are off.

    Output:

    Coin Count Value (in cents)
    Quarters: 3 $0.75
    Dimes: 3 $0.30000000000000004
    Nickels: 3 $0.15000000000000002
    Pennies: 3 $0.03
    Total: $1.2299999

    Code:

    public class Assig02_Part4
    {

    public static void main(String[] args)
    {

    Scanner scan = new Scanner (System.in);
    int quarter, dime, nickel, penny;
    float total = 0;

    System.out.print("Number of quarters: ");
    quarter = scan.nextInt();
    System.out.print("Number of dimes: ");
    dime = scan.nextInt();
    System.out.print("Number of nickels: ");
    nickel = scan.nextInt();
    System.out.print("Number of pennies: ");
    penny = scan.nextInt();

    System.out.println("Coin\tCount\tValue (in cents)");
    System.out.println("Quarters:\t" + quarter + "\t$" + (quarter * 0.25));
    total += (quarter * 0.25);
    System.out.println("Dimes:\t" + dime + "\t$" + (dime * 0.1));
    total += (dime * 0.1);
    System.out.println("Nickels:\t" + nickel + "\t$" + (nickel * 0.05));
    total += (nickel * 0.05);
    System.out.println("Pennies:\t" + penny + "\t$" + (penny * 0.01));
    total += (penny * 0.01);
    System.out.println("Total:\t\t$" + total);
    }

    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Inaccurate?!

    You should consider changing their data type from
    Code:
    int
    to
    Code:
    double
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width