我从一定数量的钱中提取硬币数量(数十,五,一,四分之一)。我有这个代码,但输出应该是:10个10比索硬币和1个1比索硬币(没有0个5比索硬币)
double money = 121;
int tens = 0;
int fives = 0;
int ones = 0;
while (money !=0) {
tens = (int) money / 10;
money = money % 10;
System.out.printlm(tens + "pcs of 10 peso coin");
fives = (int) money / 5;
money = money % 5;
System.out.printlm(fives + "pcs of 5 peso coin");
ones = (int) money % 1;
money = money % 1;
System.out.printlm(ones + "pcs of 1 peso coin");
}
输出:
12 pcs of 10 peso coin
0 pcs of 5 peso coin
1 pcs of 1 peso coin
所需的输出:
12 pcs of 10 peso coin
1 pcs of 1 peso coin.
答案 0 :(得分:3)
在每个if
语句之前添加println
条件。
例如:
...
if (tens > 0)
System.out.println(tens + "pcs of 10 peso coin");
...
if (fives > 0)
System.out.println(fives + "pcs of 5 peso coin");
...
if (ones > 0)
System.out.println(ones + "pcs of 1 peso coin");
...
答案 1 :(得分:0)
这会更有效率。无需评估不必要的金钱价值。
EmailReceiverService