基本上,1.26美元如何变成:
Dollars: 1
Quarters: 1
Pennies: 1
谢谢!编辑剩余部分的标题而不是模数运算符
int change = (int)(changeDue*100);
int dollars = (int)(change/100);
change=change%100;
int quarters = (int)(change/25);
change=change%25;
int pennies = (int)(change/1);
change=change%1
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Pennies: " + pennies);
答案 0 :(得分:0)
对于每个面额,你需要进行整数除法,然后模数:
整数除法忽略了一个除法的提醒,例如100分之126= 1
Modulo仅返回分组的提醒,例如126%100 = 26
所以126美分需要1美元。并对每个较小面额的提醒值重复相同的事情。对于季度,您将得到26/25 = 1季度,提醒是26%1,这是1 ...你明白了。
答案 1 :(得分:0)
模运算给出了除法的余数。
%-operation a % m
中的值始终从0
到m - 1
。
change=change%100; // divides by 100 and leaves the remainder which is less than a dollar
change=change%25; // divides by 25 and leaves the remainder which is less than a quarter
change=change%10; // divides by 10 and leaves the remainder which is less than a dime
change=change%5; // divides by 5 and leaves the remainder which is less than a nickel
change=change%1 // this operation is always 0 because an integer can always be divided by 1 without a remainder