我必须编写一个 Java 程序,告诉您对于 1 美分到 99 美分之间的任意金额的找零,要发放哪些硬币。例如,如果金额为 86 美分,则输出将类似于以下内容:
86 美分可以分为 3 个 25 美分、1 个角硬币和 1 个便士。
使用 25、10、5 和 1 面额的硬币。您的程序将使用以下方法(以及其他方法):
public static int computeCoin(int coinValue,);
// Precondition: 0 < coinValue < 100;
// Postcondition: returned value has been set equal to the maximum
//number of coins of the denomination coinValue cents that can be
//obtained from amount (a different variable) cents. amount has been
//decreased by the value of the coins, that is, decreased by
//returnedValue*coinValue.
到目前为止,这就是我所拥有的,但我认为我还缺少更多有人可以帮助我吗?
而且我也不应该使用双精度数来代替 int。
public class Assignment6{
public static void main(String [] args){
amount = (int)(Double.parseDouble(args[0])*100);
System.out.println("Five: " + computeCoin(500));
System.out.println("one: " + computeCoin(100) );
System.out.println("Q : " + computeCoin(25) );
System.out.println("D : " + computeCoin(10) );
System.out.println("N : " + computeCoin(5) );
System.out.println("P : " + computeCoin(1) );
}
public class Assignment6 {
private static int amount = 0;
public static void main(String[] args) {
amount = (int)(Double.parseDouble(args[0])*100);
System.out.println("Five: " + computeCoin(500));
System.out.println("one: " + computeCoin(100) );
System.out.println("Q : " + computeCoin(25) );
System.out.println("D : " + computeCoin(10) );
System.out.println("N : " + computeCoin(5) );
System.out.println("P : " + computeCoin(1) );
}
public static int computeCoin(int cointValue) {
int val = amount / cointValue;
amount -= val * cointValue;
return val;
}
}
这里的技巧在于computeCoin
方法,事实上除法是整数除法,所以val
将持有给定价值的“最大”数量的硬币(coinValue
) 其总价值不超过amount
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)