使用最低数量的硬币更改总价值,同时排除特定硬币,并且 return 结果为字符串
Changing total value using the lowest amount of coins, whilst excluding a specific coin, and return result as String
我的一项作业遇到问题,作业的一小部分是为以下描述编写方法:
A method that takes two values; the value to exchange, and the coin type to exclude, and then return the minimum coins needed to exchange the for the total value, and return the output as a String. For example changeCalculator (555,50) may return "the coins to exchange are : 2 x 200p, 1 x 100p, 0x50, 2 x 20p, 1 x 10p, with a remainder of 5p".
我能够编写代码,但是我编写的代码在循环中有 System.out.print,并且我无法在返回字符串类型时使代码工作,因为我使用的是循环。
关于我的代码,你需要知道的全部都在代码的开头 class 我已经在构造函数中放入并初始化了硬币列表:
private List<Integer> coinList = new ArrayList<Integer>();
下面是我的代码:
public void changeCalaculator (int totalCoinValue, int excludedCoinType)
{
System.out.print("The coins exchanged are: ");
for (int coin : coinList)
{
if (excludedCoinType == coin)
{
continue;
}
else
{
System.out.print(totalCoinValue/coin + " x " + coin + "p, ");
totalCoinValue = totalCoinValue%coin;
}
}
System.out.print(" with a reminader of " + totalCoinValue + "p");
}
- 对于 return 一个
String
的方法,您首先需要更改方法声明以表明该方法确实具有 return 类型。
- 此外,无需打印出结果,只需在此过程中构建一个 String 并 return 在您的方法完成后创建它。在下面的示例中,我为此任务使用了
StringBuilder
。
- 最后,添加排除的硬币类型时,您遗漏了分支的输出。 (这里不需要
continue;
,因为一旦 if
分支为真,循环中就没有其他事情可做)
更新示例:
public String multiCoinCalulator(int totalCoinValue, int excludedCoinType) {
StringBuilder sb = new StringBuilder();
sb.append("The coins exchanged are: ");
for (int coin : coinList) {
if (excludedCoinType == coin) {
sb.append(0 + " x " + coin + "p, ");
} else {
sb.append(totalCoinValue / coin + " x " + coin + "p, ");
totalCoinValue = totalCoinValue % coin;
}
}
sb.append(" with a remainder of " + totalCoinValue + "p");
return sb.toString();
}
我的一项作业遇到问题,作业的一小部分是为以下描述编写方法:
A method that takes two values; the value to exchange, and the coin type to exclude, and then return the minimum coins needed to exchange the for the total value, and return the output as a String. For example changeCalculator (555,50) may return "the coins to exchange are : 2 x 200p, 1 x 100p, 0x50, 2 x 20p, 1 x 10p, with a remainder of 5p".
我能够编写代码,但是我编写的代码在循环中有 System.out.print,并且我无法在返回字符串类型时使代码工作,因为我使用的是循环。
关于我的代码,你需要知道的全部都在代码的开头 class 我已经在构造函数中放入并初始化了硬币列表:
private List<Integer> coinList = new ArrayList<Integer>();
下面是我的代码:
public void changeCalaculator (int totalCoinValue, int excludedCoinType)
{
System.out.print("The coins exchanged are: ");
for (int coin : coinList)
{
if (excludedCoinType == coin)
{
continue;
}
else
{
System.out.print(totalCoinValue/coin + " x " + coin + "p, ");
totalCoinValue = totalCoinValue%coin;
}
}
System.out.print(" with a reminader of " + totalCoinValue + "p");
}
- 对于 return 一个
String
的方法,您首先需要更改方法声明以表明该方法确实具有 return 类型。 - 此外,无需打印出结果,只需在此过程中构建一个 String 并 return 在您的方法完成后创建它。在下面的示例中,我为此任务使用了
StringBuilder
。 - 最后,添加排除的硬币类型时,您遗漏了分支的输出。 (这里不需要
continue;
,因为一旦if
分支为真,循环中就没有其他事情可做)
更新示例:
public String multiCoinCalulator(int totalCoinValue, int excludedCoinType) {
StringBuilder sb = new StringBuilder();
sb.append("The coins exchanged are: ");
for (int coin : coinList) {
if (excludedCoinType == coin) {
sb.append(0 + " x " + coin + "p, ");
} else {
sb.append(totalCoinValue / coin + " x " + coin + "p, ");
totalCoinValue = totalCoinValue % coin;
}
}
sb.append(" with a remainder of " + totalCoinValue + "p");
return sb.toString();
}