# 存储总硬币价值，使用最少数量的硬币？

go评论57阅读模式

Store total coin value using lowest amount of coins?

# 问题

``````public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("需要交换的硬币为：");
for (int c : coinList) {
if (c == coinValue) {
System.out.println("0 " + c + "p");
} else {
int result = (coin / c) * c;
System.out.println(result / c + " " + c + "p。");
}
}
}
``````

``````需要交换的硬币为：
3 200p。
7 100p。
15 50p。
37 20p。
75 10p。
``````

I am trying to write a code that reflects the below instructions.

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 multiCoinCalculator (756,50) may return "the coins to exchange are : 3 x 200p, 1 x 100p, 0x50, 2 x 20p, 1 x 10p, with a remainder of 6p".

The code I have written here returns how much of each coin is needed to make that value.

``````public void multiCoinCalculator(int coin, int coinValue) {
System.out.println(&quot;The exchanged coins are: &quot;);
for (int c : coinList) {
if ( c == coinValue) {
System.out.println(&quot;0 &quot; + c + &quot;p&quot;);
}

else {
int result = (coin/c)*c;
System.out.println(result/c + &quot; &quot; + c + &quot;p. &quot;);

}

}
``````

When I enter 756 as coin value and 50 as excluded coin I get the following:

``````The exchanged coins are:
3 200p.
7 100p.
15 50p.
37 20p.
75 10p.
``````

Would it be possible to get any advice on how to fix this?

# 答案1

``````public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("The exchanged coins are: ");
for (int c : coinList) {
if (c == coinValue) {
System.out.println("0 " + c + "p");
} else {
int result = (coin / c);
System.out.println(result + " " + c + "p. ");
coin = coin % c;
}
}
System.out.println("Remainder of ," + coin + "p. ");
}
``````

``````public void multiCoinCalculator(int coin, int coinValue) {
System.out.println(&quot;The exchanged coins are: &quot;);
for (int c : coinList) {
if ( c == coinValue) {
System.out.println(&quot;0 &quot; + c + &quot;p&quot;);
}

else {
int result = (coin/c);
System.out.println(result + &quot; &quot; + c + &quot;p. &quot;);
coin=coin%c
}
}
System.out.println(&quot;Remainder of ,&quot;+ coin +&quot;p. &quot;)
``````

• 本文由 发表于 2020年10月2日 01:02:41
• 转载请务必保留本文链接：https://go.coder-hub.com/64160091.html
• java
• loops

go 61

go 59

go 65

go 62