作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个输出零钱的代码,如下所示:
**$7.00 remains to be paid. Enter coin or note: $100.00
You gave $100.00
Your change:
1 x $50.00
2 x $20.00
0 x $10.00
0 x $5.00
1 x $2.00
1 x $1.00
**
但输出应该是相同的,但不显示 0 值,例如它应该是这样的:
**$7.00 remains to be paid. Enter coin or note: $100.00
You gave $100.00
Your change:
1 x $50.00
2 x $20.00
1 x $2.00
1 x $1.00
**
我的货币兑换代码看起来像这样,使用 if 语句:
double fiftyDollars, twentyDollars, tenDollars, fiveDollars, twoDollars, oneDollar, fiftyCents, twentyCents, tenCents, fiveCents;
fiftyDollars = change / 50.00; //dividing change by
change = change % 50.00; //get remainder of the change
if(fiftyDollars > 0) {
System.out.println((int) fiftyDollars + " x $50.00"); //getting output as integer- casting
}
twentyDollars = change / 20.00;
change = change % 20.00;
if (twentyDollars > 0){
System.out.println((int) twentyDollars + " x $20.00");
}
tenDollars = change / 10.00;
change = change % 10.00;
if (tenDollars >0) {
System.out.println((int) tenDollars + " x $10.00");
}
fiveDollars = change / 5.00;
change = change % 5.00;
if(fiveDollars > 0) {
System.out.println((int) fiveDollars + " x $5.00");
}
twoDollars = change / 2.00;
change = change % 2.00;
if (twoDollars >0) {
System.out.println((int) twoDollars + " x $2.00");
}
oneDollar = change / 1.00;
change = change % 1.00;
if(oneDollar >0) {
System.out.println((int) oneDollar + " x $1.00");
}
fiftyCents = change / 0.5;
change = change % 0.5;
if (fiftyCents >0) {
System.out.println((int) fiftyCents + " x $0.50");
}
twentyCents = change / 0.2;
change = change % 0.2;
if(twentyCents > 0) {
System.out.println((int) twentyCents + " x $0.20");
}
tenCents = change / 0.1;
change = change % 0.1;
if (tenCents > 0){
System.out.println((int) tenCents + " x $0.10");
}
fiveCents = change / 0.05;
change = change % 0.05;
if (fiveCents > 0){
System.out.println((int) fiveCents + " x $0.05");
}
非常感谢你们的帮助!谢谢您
最佳答案
您可以检查是否等于或大于 1 的值。
正如另一个答案中所述,您正在处理 double
值,因此 >0
将匹配 0.1
、 0.56
等..
所以:
if (tenDollars >= 1)
关于JAVA货币兑换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36356147/
为了实现促销代码,我执行了以下步骤: 在 Google Play Console>User Acquisition>Promotions 我添加了促销 Activity 在应用程序中,我使用它来将用户
我希望能够生成链接,允许我的 iOS 应用程序的用户兑换应用程序内购买的促销代码。因此,他们可能会收到一封带有“兑换代码”按钮的电子邮件,该按钮会将他们带到应用商店兑换页面,其中预填充了代码,或者为他
从最近的版本和下面的对话来看,它说现在 Katana(4.1.0) 支持代码流和自动代码赎回(这意味着我们没有明确调用 tokenendpoint 来赎回 idtoken、accesstoken 等的
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我是一名优秀的程序员,十分优秀!