2016-11-29 63 views
0

需要幫助弄清楚如何打印個人硬幣,如宿舍= 3,便士= 1,而不是給我4個硬幣76美分。我嘗試設置4個計數器,但只是反覆打印出硬幣名稱和答案是錯誤的。遞歸幣改變

import java.util.Scanner; 
public class Money 
{ 
    public static void main(String args[]) 
    { 
     int[] coins = { 1, 5, 10, 25}; 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter Change (In Cents): "); 
     int sum = scan.nextInt(); 
     int counter1 = 0; 
     int counter2 = 0; 
     int counter3 = 0; 
     int counter4 = 0; 
     String quarter = ""; 
     Money minCoin = new Money(); 
     System.out.println(minCoin.findMinCoins(coins, sum, counter1, counter2, counter3, counter4)); 
     System.out.println(counter4); 
    } 

    private int findMinCoins(int[] coins, int sum, int counter1, int counter2, int counter3, int counter4) 
    { 
     if (sum <= 0 || coins.length == 0) 
     { 
      return 0; 
     } 

     for (int i = coins.length - 1; i >= 0; i--) 
     { 
      if(coins[i] == 1 && coins[i] != 5) 
      { 
       counter1++; 
      } 

      if(coins[i] == 5) 
      { 
       counter2++; 
      } 

      if(coins[i] == 10) 
      { 
       counter3++; 
      } 

      if(coins[i] == 25) 
      { 
       counter4++; 
      } 

      if (coins[i] <= sum) 
      { 
       System.out.println("Pennies: " + counter1); 
       System.out.println("Nickels: " + counter2); 
       System.out.println("Dimes: " + counter3); 
       System.out.println("Quarters: " + counter4); 
       return 1 + findMinCoins(coins, sum - coins[i], counter1, counter2, counter3, counter4); 

      } 
     } 
     return 0; 
    } 

} 
+0

在您的退貨聲明中,您有1 + findMinCoins ...但這並不真正提供關於1表示什麼樣的硬幣的信息。相反,嘗試向return語句中的變量添加1,例如:findMinCoins(coins,sum - coins [i],counter1 + 1,...)以表明您需要另一個便士 –

回答

0

試試這個:

import java.util.Scanner; 
public class Money 
{ 
public static void main(String[] args) { 

     int[] coins = {1, 5, 10, 25}; 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter Change (In Cents): "); 
     int sum = scan.nextInt(); 
     int counter1 = 0; 
     int counter2 = 0; 
     int counter3 = 0; 
     int counter4 = 0; 
     String quarter = ""; 
     Money minCoin = new Money(); 
     System.out.println(minCoin.findMinCoins(coins, sum, counter1, counter2, counter3, counter4)); 
     System.out.println(counter4); 

    } 

    private int findMinCoins(int[] coins, int sum, int counter1, int counter2, int counter3, int counter4) { 
     if (sum <= 0 || coins.length == 0) { 
      return 0; 
     } 

     for (int i = coins.length - 1; i >= 0; i--) { 

      if (coins[i] == 25 && sum >= coins[i]) { 
       counter4++; 

      } 

      else if (coins[i] == 10 && sum >= coins[i]) { 
       counter3++; 
      } 

      else if (coins[i] == 5 && sum >= coins[i]) { 
       counter2++; 
      } 

      else if (coins[i] == 1 && sum >= coins[i]) { 
       counter1++; 
      } 

      if (coins[i] <= sum) { 
       System.out.println("Pennies: " + counter1); 
       System.out.println("Nickels: " + counter2); 
       System.out.println("Dimes: " + counter3); 
       System.out.println("Quarters: " + counter4); 
       return 1 + findMinCoins(coins, sum - coins[i], counter1, counter2, counter3, counter4); 

      } 
     } 
     return 0; 
    } 
} 
+0

請考慮添加一些附加信息你的答案解釋你的變化。 :) – Gulllie

+0

在這裏,我添加了硬幣和總和的價值之間的比較。 – RJMIMI38

0

我懷疑你誤會傳遞給Java方法的參數是如何工作的。您正在傳入'counter'值,然後在方法內迭代它們,並期望它們在調用方法中的值已更改。這不是Java的工作原理。有關更多詳細信息,請參閱here

如果您不希望從方法中獲取值(因爲您在打印它們),那麼將它們作爲參數沒有意義。最好將其作爲方法範圍內的局部變量。

如果您從最大到最小的順序並從您的更改計算器返回一個數組,您可能會發現它更容易一些。那麼你不需要你的計數器的個別變量。

類似以下內容:

int[] coins = {25, 10, 5, 1}; 
String[] names = {"Quarters", "Dimes", "Nickles", "Pennies"}; 
int[] change = getChange(coins, total); 
for (int i = 0; i < change.length; i++) { 
    System.out.println(names[i] + ":" + change[i]); 
} 

private int[] getChange(int[] coins, int total) { 
    int[] result = new int[coins.length]; 
    for (int i = 0; i < coins.length; i++) { 
     result[i] = total/coins[i]; 
     total -= result[i] * coins[i]; 
    } 
    return result; 
} 

請注意,這需要Java的整數除法的優勢捨去。所以如果你以76美分開始,它將返回3個季度(76/25 - > 3)。還要注意,循環中的第二條語句可能是total %= coins[i],它可能具有相同的效果,但可能更容易理解。無論哪種方式,代碼說'從當前面額的總價值中刪除'。