以下是我必须做的事情:
编写一个程序,对少于一美元的金额进行更改。程序的输入必须是小于100的正整数,表示金额,以美分为单位。输出必须是原始金额以及可以弥补金额的一组硬币(四分之一,硬币,镍币)。该程序必须产生包含产生给定数量所需的最小硬币数量的变化。 不应包含任何便士。例如,54的输入应产生如下结果:
54美分需要2个季度,1个镍
而不是
54美分需要2个季度,0角钱,1个镍币
以下是我到目前为止所做的事情,但如果我输入13,14,23,24,33,34等,则不会进行整理。
非常感谢任何帮助。
import java.util.Scanner;
public class MakeChangetest {
public static String makeChange(int change) throws BadChangeException {
int amount;
String x = "";
if (change > 0 && change < 100) {
amount = (int) ((Math.round(change / 5)) * 5);
if (amount == 0 || amount == 100) {
x = "No change to be given.";
}
if (amount == 5) {
x = change + " cents requires " + " 1 nickel ";
} else if (amount == 10 || amount == 20) {
x = change + " cents requires " + (amount / 10) + " dimes ";
} else if (amount == 15) {
x = change + " cents requires " + (amount / 10) + " dime, "
+ " 1 nickel ";
} else if (amount == 25 || amount == 50 || amount == 75) {
x = change + " cents requires " + (amount / 25) + " quaters ";
} else if (amount == 30 || amount == 55 || amount == 80) {
x = change + " cents requires " + (amount / 25) + " quaters, "
+ " 1 nickel";
} else if (amount == 35 || amount == 60 || amount == 70 || amount == 45 || amount == 85
|| amount == 95) {
if (amount % 25 == 10) {
x = change + " cents requires " + (amount / 25) + " quater, "
+ "1 dime ";
} else
x = change + "cents requires " + (amount / 25) + " quaters, "
+ " 2 dimes ";
} else if (amount == 40 || amount == 65 || amount == 90) {
if (amount / 25 == 15) {
x = change + " cents requires " + (amount / 25) + " quater, "
+ " 1 dime, " + " 1 nickel ";
} else
x = change + " cents requires " + (amount / 25) + " quaters, "
+ " 1 dime, " + " 1 nickel ";
}
} else
throw new BadChangeException(
"Amount is not in the range to be given change for.");
return x;
}
public static void main(String[] args) throws BadChangeException {
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount to calculate the amount of change to be given:");
double t = input.nextDouble();
try {
System.out.println(makeChange((int) t));
} catch (BadChangeException ex) {
System.out.print("Amount is not in the range to be given change for.");
}
}
}
答案 0 :(得分:1)
当您编写change / 5
时,change
和5
都是整数,因此Java会执行整数除法。整数除法向下舍入,例如:
(Math.round(14 / 5)) * 5 => (Math.round(2)) * 5 => 10
要解决此问题,您需要将其中一个操作数作为浮点数:
amount = (int) ((Math.round(change / 5.0)) * 5);
或
amount = (int) ((Math.round(change / 5.0f)) * 5);
或
amount = (int) ((Math.round((float)change / 5)) * 5);
答案 1 :(得分:0)
你有很多if语句,而是使用开关。我会用一点清理代码在一秒钟内更新这个答案。
这里有一些清理过的代码,其中包含来自其他答案的修复程序:
public class MakeChangetest {
public static String makeChange(int change) throws BadChangeException {
int amount;
String x = "";
if (0 <= change <= 100) {
amount = (int) ((Math.round(change / 5.0)) * 5);
switch(amount) {
case 0:
case 100:
x = "No change to be given.";
break;
case 5:
x = change + " cents requires " + " 1 nickel ";
break;
case 10:
case 20:
x = change + " cents requires " + (amount / 10) + " dimes ";
break;
case 15:
x = change + " cents requires " + (amount / 10) + " dime, " + " 1 nickel ";
break;
case 25:
case 50:
case 75:
x = change + " cents requires " + (amount / 25) + " quaters ";
break;
case 30:
case 55:
case 80:
x = change + " cents requires " + (amount / 25) + " quaters, " + " 1 nickel";
brea;
case 35:
case 60:
case 70:
case 45:
case 85:
case 95:
if (amount % 25 == 10) {
x = change + " cents requires " + (amount / 25) + " quater, " + "1 dime ";
} else {
x = change + "cents requires " + (amount / 25) + " quaters, " + " 2 dimes ";
}
break;
case 40:
case 65:
case 90:
if (amount / 25 == 15) {
x = change + " cents requires " + (amount / 25) + " quater, " + " 1 dime, " + " 1 nickel ";
} else {
x = change + " cents requires " + (amount / 25) + " quaters, " + " 1 dime, " + " 1 nickel ";
}
}
} else
throw new BadChangeException(
"Amount is not in the range to be given change for.");
return x;
}
public static void main(String[] args) throws BadChangeException {
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount to calculate the amount of change to be given:");
double t = input.nextDouble();
try {
System.out.println(makeChange((int) t));
} catch (BadChangeException ex) {
System.out.print("Amount is not in the range to be given change for.");
}
}
}
因此,总是试图远离嵌套多个if else语句,事情变得混乱,难以快速阅读。
您也可以将(0 < change && change < 100)
写为(0 < change < 100)
。
由于(0 < change < 100)
不包含0
和100
,因此代码无法正常运行,但实际上您需要包含这些代码,因为您需要检查0
和下面代码中的100
。
所以它应该是(0 <= change <= 100)
。