Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How do I correct this calculator problem?

$
0
0
I was able to put together this program. But it does not give me the number of pennies from $.36 which should be one. Can anyone point me to what I may be doing wrong? I am not supposed to include nickels as a parameter. Thank you.

import java.util.Scanner;
public class main {
	public static void main(String[] args) {
		final double PENNY = 0.01;
		final double DIME = 0.10;
		final double QUARTER = 0.25;
		System.out.print("How much change do you have?  ");
		Scanner sc = new Scanner(System.in);
		double received = sc.nextDouble();
		double price = 0;
		double change = received - price;
		System.out.format("Change due: $%.2f\n", change);
		System.out.println("Your coin breakdown is: ");
		int n;
		for(n = 0; change > QUARTER; n++) change -= QUARTER;
		System.out.format("Quarters: %d\n", n);
		for(n = 0; change > DIME; n++) change -= DIME;
		System.out.format("Dimes: %d\n", n);
		for(n = 0; change >= PENNY; n++) change -= PENNY;
		System.out.format("Pennies: %d\n", n);
	}
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles