我为我的 Java 类构建的变更机器有问题。我对编程很陌生,所以这可能是一个愚蠢的逻辑错误。我的找零机要求用户输入物品的价格,然后输入支付的金额,然后它应该计算用户将收到多少硬币,硬币等......但是,它每次只计算 1 个季度。这是代码:
import java.util.Scanner;
import java.text.*;
public class Main {
public static void main(String[] args)
{
float penny = .1F, nickel = .5F, dime = .10F, quarter = .25F;
int pennyCtr = 0, nickelCtr = 0, dimeCtr = 0, quarterCtr = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter Purchase Price: ");
float price = scan.nextFloat();
DecimalFormat paidFormat = new DecimalFormat("0.00");
System.out.println("Enter Amount Paid: ");
float paid = scan.nextFloat();
float change = (float) (paid - price);
System.out.println("Your change from $" + paidFormat.format(paid)
+ " is: " + change);
if (change >= .25)
{
change -= quarter;
quarterCtr++;
}
else if (change < .25)
{
change -= dime;
dimeCtr++;
}
else if (change < .10)
{
change -= nickel;
nickelCtr++;
}
else if (change < .5)
{
change -= penny;
pennyCtr++;
}
System.out.println("Your change from $" + paidFormat.format(paid) +
" is: " + quarterCtr + " Quarters, " + dimeCtr + " Dimes, "
+ nickelCtr + " Nickles, " + pennyCtr + " Pennies. ");
System.out.println("Program written by Ashley ");
}
}