Changing total value using the lowest amount of coins, whilst excluding a specific coin, and return result as String

huangapple go评论56阅读模式
英文:

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&lt;Integer&gt; coinList = new ArrayList&lt;Integer&gt;();

Here is my code below:

public void changeCalaculator (int totalCoinValue, int excludedCoinType)
{
	System.out.print(&quot;The coins exchanged are: &quot;);
	for (int coin : coinList)
	{
		if (excludedCoinType == coin)
		{
			continue;
		}
		else
		{

		System.out.print(totalCoinValue/coin + &quot; x &quot; + coin + &quot;p, &quot;);		
		totalCoinValue = totalCoinValue%coin;
		}	
	} 
	System.out.print(&quot; with a reminader of &quot; + totalCoinValue + &quot;p&quot;); 
}

答案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 the if branch is true)

Updated example:

public String multiCoinCalulator(int totalCoinValue, int excludedCoinType) {
    StringBuilder sb = new StringBuilder();
    sb.append(&quot;The coins exchanged are: &quot;);
    for (int coin : coinList) {
        if (excludedCoinType == coin) {
            sb.append(0 + &quot; x &quot; + coin + &quot;p, &quot;);
        } else {
            sb.append(totalCoinValue / coin + &quot; x &quot; + coin + &quot;p, &quot;);
            totalCoinValue = totalCoinValue % coin;
        }
    }
    sb.append(&quot; with a remainder of &quot; + totalCoinValue + &quot;p&quot;);
    return sb.toString();
}

huangapple
  • 本文由 发表于 2020年9月24日 19:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64045975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定