JAVA 零钱
JAVA money change
我正在尝试编写一个输出零钱的代码,如下所示:
**.00 remains to be paid. Enter coin or note: 0.00
You gave 0.00
Your change:
1 x .00
2 x .00
0 x .00
0 x .00
1 x .00
1 x .00
**
但输出应该是相同的但不显示 0 值,例如它应该是这样的:
**.00 remains to be paid. Enter coin or note: 0.00
You gave 0.00
Your change:
1 x .00
2 x .00
1 x .00
1 x .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 .00"); //getting output as integer- casting
}
twentyDollars = change / 20.00;
change = change % 20.00;
if (twentyDollars > 0){
System.out.println((int) twentyDollars + " x .00");
}
tenDollars = change / 10.00;
change = change % 10.00;
if (tenDollars >0) {
System.out.println((int) tenDollars + " x .00");
}
fiveDollars = change / 5.00;
change = change % 5.00;
if(fiveDollars > 0) {
System.out.println((int) fiveDollars + " x .00");
}
twoDollars = change / 2.00;
change = change % 2.00;
if (twoDollars >0) {
System.out.println((int) twoDollars + " x .00");
}
oneDollar = change / 1.00;
change = change % 1.00;
if(oneDollar >0) {
System.out.println((int) oneDollar + " x .00");
}
fiftyCents = change / 0.5;
change = change % 0.5;
if (fiftyCents >0) {
System.out.println((int) fiftyCents + " x [=13=].50");
}
twentyCents = change / 0.2;
change = change % 0.2;
if(twentyCents > 0) {
System.out.println((int) twentyCents + " x [=13=].20");
}
tenCents = change / 0.1;
change = change % 0.1;
if (tenCents > 0){
System.out.println((int) tenCents + " x [=13=].10");
}
fiveCents = change / 0.05;
change = change % 0.05;
if (fiveCents > 0){
System.out.println((int) fiveCents + " x [=13=].05");
}
非常感谢你们的帮助!
谢谢
您使用的是双精度值,因此 0 值介于 0 和 1 之间,
如果您在 if 子句中将 double 转换为 ints,您的程序应该可以正常工作。
您可以检查等于或大于 1 的值。
如另一个答案所述,您正在处理 double
值,因此 >0
将匹配 0.1
、 0.56
等 ..
所以 :
if (tenDollars >= 1)
像这样更改所有 if 条件
if(fiftyDollars > 0) {
到
if((int)fiftyDollars > 0) {
我正在尝试编写一个输出零钱的代码,如下所示:
**.00 remains to be paid. Enter coin or note: 0.00
You gave 0.00
Your change:
1 x .00
2 x .00
0 x .00
0 x .00
1 x .00
1 x .00
**
但输出应该是相同的但不显示 0 值,例如它应该是这样的:
**.00 remains to be paid. Enter coin or note: 0.00
You gave 0.00
Your change:
1 x .00
2 x .00
1 x .00
1 x .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 .00"); //getting output as integer- casting
}
twentyDollars = change / 20.00;
change = change % 20.00;
if (twentyDollars > 0){
System.out.println((int) twentyDollars + " x .00");
}
tenDollars = change / 10.00;
change = change % 10.00;
if (tenDollars >0) {
System.out.println((int) tenDollars + " x .00");
}
fiveDollars = change / 5.00;
change = change % 5.00;
if(fiveDollars > 0) {
System.out.println((int) fiveDollars + " x .00");
}
twoDollars = change / 2.00;
change = change % 2.00;
if (twoDollars >0) {
System.out.println((int) twoDollars + " x .00");
}
oneDollar = change / 1.00;
change = change % 1.00;
if(oneDollar >0) {
System.out.println((int) oneDollar + " x .00");
}
fiftyCents = change / 0.5;
change = change % 0.5;
if (fiftyCents >0) {
System.out.println((int) fiftyCents + " x [=13=].50");
}
twentyCents = change / 0.2;
change = change % 0.2;
if(twentyCents > 0) {
System.out.println((int) twentyCents + " x [=13=].20");
}
tenCents = change / 0.1;
change = change % 0.1;
if (tenCents > 0){
System.out.println((int) tenCents + " x [=13=].10");
}
fiveCents = change / 0.05;
change = change % 0.05;
if (fiveCents > 0){
System.out.println((int) fiveCents + " x [=13=].05");
}
非常感谢你们的帮助! 谢谢
您使用的是双精度值,因此 0 值介于 0 和 1 之间, 如果您在 if 子句中将 double 转换为 ints,您的程序应该可以正常工作。
您可以检查等于或大于 1 的值。
如另一个答案所述,您正在处理 double
值,因此 >0
将匹配 0.1
、 0.56
等 ..
所以 :
if (tenDollars >= 1)
像这样更改所有 if 条件
if(fiftyDollars > 0) {
到
if((int)fiftyDollars > 0) {