英文:
Changing total value using the lowest amount of coins, whilst excluding a specific coin, and return result as String
问题
我在我的任务中遇到了一个问题,任务的一个小部分是为以下描述编写方法:
> 编写一个方法,该方法接受两个值;要交换的值和要排除的硬币类型,然后返回进行交换所需的最小硬币数量,并将输出返回为一个字符串。例如,changeCalculator(555, 50) 可能返回 "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,而且我无法使代码在返回字符串类型的同时正常工作,因为我在使用循环。
你只需要知道我的代码,在代码类的开头,我已经放置并已经在构造函数中初始化了硬币列表:
private List<Integer> coinList = new ArrayList<Integer>();
以下是我的代码:
public void changeCalculator(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 remainder of " + totalCoinValue + "p");
}
英文:
I am facing an issue with one of my assignments, where a small part of the assignment is writing method for the below description:
> 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".
I was able to write up a code, but the code I have written has System.out.print in the loop, and I am unable to make the code work whilst returning a string type, as I am using a loop.
All you need to know for my code, is at the start of the code class I have put and have already initialized the list of coins in the constructor:
private List<Integer> coinList = new ArrayList<Integer>();
Here is my code below:
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");
}
答案1
得分: 0
以下是翻译好的部分:
- 要使您的方法返回一个
String
,首先需要更改您的方法声明,以显示该方法实际上具有返回类型。 - 此外,不要打印出结果,只需在方法执行过程中逐步构建一个字符串,并在方法完成后返回它。在以下示例中,我使用了
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();
}
英文:
- For your method to return a
String
you firstly need to change your method declaration to show that the method actually does have a return type. - Furthermore, instead of printing out the results, just build up a String along the way and return it after your method is finished. In the following example I used a
StringBuilder
for this task. - Lastly, you were missing the output from the branch when your excluded coin type is added. (You don't need the
continue;
here, because there is nothing else to do in the loop, once theif
branch is true)
Updated example:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论