PDA

Click to See Complete Forum and Search --> : Inaccurate?!


guitarhero14
Feb 7th, 2006, 08:34 PM
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);
}

}

ComputerJy
Feb 8th, 2006, 07:08 AM
You should consider changing their data type from int to double