以下代码并未计算在Java“收银机”中分发更改的最佳方式。我该如何解决?
public void generateUSDChange(){
change = payment-purchase;
quarters = (int) (change/.25);
change -= (quarters * .25);
dimes = (int) (change/.1);
change -= (dimes * .1);
nickels = (int) (change/.05);
change -= (nickels * .05);
pennies = (int) (change/.01);
changeamount = quarters*.25 + dimes*.1 + nickels*.05 + pennies*.01;
if(changeamount != (payment-purchase)){
pennies++;
if(pennies>=5){
nickels++;
pennies-=5;
}
if(nickels>=2){
dimes++;
nickels-=2;
}
if(((dimes*.1) + (nickels*.05)) >= .25){
quarters++;
dimes-=2;
nickels--;
}
}
}
答案 0 :(得分:0)
其他人也建议双重和浮动不利于货币。您可以使用BigDecimal。这是工作代码:
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
public enum CurrencyDenomination {
HUNDRED(BigDecimal.valueOf(100)), FIFTY(BigDecimal.valueOf(50)), TWENTY(BigDecimal.valueOf(20)),
TEN(BigDecimal.valueOf(10)), FIVE(BigDecimal.valueOf(5)), ONE(BigDecimal.valueOf(1)),
QUARTER(BigDecimal.valueOf(.25)), DIME(BigDecimal.valueOf(.10)),
NICLE(BigDecimal.valueOf(.05)), PENNIES(BigDecimal.valueOf(.01));
private BigDecimal value;
CurrencyDenomination(BigDecimal value) {
this.value = value;
}
public static Map<CurrencyDenomination, Integer> calculate(BigDecimal balance) {
Map<CurrencyDenomination, Integer> balanceCurrency = new LinkedHashMap<CurrencyDenomination, Integer>();
BigDecimal leftOver = balance;
System.out.println("Given amount : "+balance);
for (CurrencyDenomination currencyDenomination : CurrencyDenomination
.values()) {
int count = leftOver.divide(currencyDenomination.value).intValue();
if (leftOver != BigDecimal.ZERO) {
if (balanceCurrency.containsKey(currencyDenomination)) {
int existingCount = balanceCurrency
.get(currencyDenomination);
existingCount = existingCount + count;
balanceCurrency.put(currencyDenomination, existingCount);
} else {
balanceCurrency.put(currencyDenomination, count);
}
}
leftOver = leftOver.remainder(currencyDenomination.value);
if (leftOver.equals(BigDecimal.ZERO)) {
break;
}
}
return balanceCurrency;
}
public static void main(String[] args) {
System.out.println("BalanceCurrency : "+calculate(BigDecimal.valueOf(49.52)));
}
}
输出:
Given amount : 49.52
BalanceCurrency : {HUNDRED=0, FIFTY=0, TWENTY=2, TEN=0, FIVE=1, ONE=4, QUARTER=2, DIME=0, NICLE=0, PENNIES=2}
答案 1 :(得分:0)
我同意上述答案和关于使用BigDecimal的评论。处理它的另一种方法是使用代表美分的整数值,假设您使用的是美元货币。你可以尝试这样的事情。 (货币金额只是简单测试的示例)
public void generateUSDChange(){
int payment = 500; //$5.00
int purchase = 268; //$2.68
int change = payment - purchase;
int quarters = change / 25;
change = change % 25;
int dimes = change / 10;
change = change % 10;
int nickles = change / 5;
int pennies = change % 5;
System.out.println("Your change is " + quarters + " quarters " + dimes + " dimes " + nickles + " nickles, and "+ pennies + " pennies.");
int change = pennies * 1 + nickles * 5 + dimes * 10 + quarters * 25;
System.out.printf("Your change totals to $%d.%02d", change/100, change%100);}