作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在为学校做一项作业,我需要编写一个程序,需要在此模式下返回您的零钱
输入:购买金额: $12.45给定金额:$15.00
输出:找零:2.55 美元2 张一美元钞票2个季度1 镍币
但是,如果我处理这种情况,它就可以正常工作。现在,如果我想让这个场景发挥作用,我会得到什么
输入:购买金额: $19.51给定金额:$50.01
输出:
二十美元钞票1十元钞票:1张宿舍:1一毛钱:2便士:4
这是不正确的,因为我返回给客户的是 30.49 而不是 30.5
无法弄清楚这里出了什么问题。
谢谢
boolean isFiveBillOn = false, isTwentybillOn = false, isTenbillOn = false, isDollarOn = false, isQuartersOn = false, isDimesOn = false, isNicklesOn = false, isPenniesOn = false;
//retrieve amount due
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
double amountd = Double.parseDouble(moneyd);
String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
double amountr = Double.parseDouble(moneyr);
//calculate difference
double difference = (amountr - amountd);
//convert to pennies
int balance = (int)(difference * 100);
//twenty dollar bill
int twentydollars = balance / 2000;
balance = (balance % 2000);
if (twentydollars > 0) {
isTwentybillOn = true;
}
//ten dollar bill
int tendollars = balance / 1000;
balance = (balance % 1000);
if (tendollars > 0) {
isTenbillOn = true;
}
//five dollar bill
int fivedollars = balance / 500;
balance = (balance % 500);
if (fivedollars > 0) {
isFiveBillOn = true;
}
//dollar bill
int dollars = balance / 100;
balance = (balance % 100);
if (dollars > 0) {
isDollarOn = true;
}
//quartes
int quarters = balance / 25;
balance = (balance % 25);
if (quarters > 0) {
isQuartersOn = true;
}
//dimes
int dimes = balance / 10;
balance = (balance % 10);
if (dimes > 0) {
isDimesOn = true;
}
//nickles
int nickles = balance / 5;
balance = (balance % 5);
if (nickles > 0){
isNicklesOn = true;
}
//pennies
int pennies = balance / 1;
balance = (balance % 1);
if (pennies > 0){
isPenniesOn = true;
}
//checking for difference over 0
if (difference < 0) {
JOptionPane.showMessageDialog(null, "The amount received can't be less than the amount owned");
} else {
// Returning Message
} if (isTwentybillOn) {
System.out.println("Twenty dollar bill " + twentydollars);
} if (isTenbillOn) {
System.out.println("Ten dollar bill: " + tendollars);
} if (isDollarOn) {
System.out.println("One Dollar Bill: " + dollars);
} if (isQuartersOn) {
System.out.println("Quarters: " + quarters);
} if (isDimesOn) {
System.out.println("Dimes: " + dimes);
} if (isNicklesOn) {
System.out.println("Nickles: " + nickles);
} if (isPenniesOn) {
System.out.println("Pennies: " + pennies);
} else {
System.out.println("Thanks! have a good day");
}
}
最佳答案
您永远不应该使用double
或任何其他 float 据类型来表示金钱。原因是它们不能准确地表示所有值。例如,以下行:
System.out.println(new BigDecimal(0.1));
打印
0.1000000000000000055511151231257827021181583404541015625
有关详细原因的解释,请参阅 What Every Computer Scientist Should Know About Floating-Point Arithmetic .
我建议使用BigDecimal
反而。
如果您只想使用基元,另一种方法是使用 int
来存储以美分为单位的货币值,并在显示该值时除以 100。
关于java - 如何在Java中正确返回金钱双变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390830/
我是一名优秀的程序员,十分优秀!