2012-06-21 37 views
2

我必須編寫一個Java程序,告訴硬幣發出1美分到99美分的任何變化量。例如,如果金額爲86美分,則輸出將如下所示:Java程序,告訴硬幣發出1美分到99美分的任何數量變化

86美分可以作爲3個季度,1美分和1美分給出。

的25使用硬幣面值,10,5,和1。你的程序將使用以下方法(其中包括):

public static int computeCoin(int coinValue,); 
// Precondition: 0 < coinValue < 100; 
// Postcondition: returned value has been set equal to the maximum 
//number of coins of the denomination coinValue cents that can be 
//obtained from amount (a different variable) cents. amount has been 
//decreased by the value of the coins, that is, decreased by  
//returnedValue*coinValue. 

到目前爲止,這是我,但我覺得我是錯過更多可以有人給我一隻手? 而且我也不會使用雙打代替int。

public class Assignment6{ 
    public static void main(String [] args){ 
    amount = (int)(Double.parseDouble(args[0])*100); 

    System.out.println("Five: " + computeCoin(500)); 
    System.out.println("one: " + computeCoin(100)); 
    System.out.println("Q : " + computeCoin(25)); 
    System.out.println("D : " + computeCoin(10)); 
    System.out.println("N : " + computeCoin(5)); 
    System.out.println("P : " + computeCoin(1)); 
} 
+0

您需要將'amount'設爲全局變量。 – arshajii

+0

我該怎麼做? – pasito15

+0

就我所見,您從未使用您存儲在金額內的值。並且可以請您提供computeCoing功能或至少告訴它做了什麼? –

回答

4
public class Assignment6 { 
    private static int amount = 0; 
    public static void main(String[] args) { 
     amount = (int)(Double.parseDouble(args[0])*100); 
     System.out.println("Five: " + computeCoin(500)); 
     System.out.println("one: " + computeCoin(100)); 
     System.out.println("Q : " + computeCoin(25)); 
     System.out.println("D : " + computeCoin(10)); 
     System.out.println("N : " + computeCoin(5)); 
     System.out.println("P : " + computeCoin(1)); 
    } 

    public static int computeCoin(int cointValue) { 
     int val = amount/cointValue; 
     amount -= val * cointValue; 
     return val; 
    } 
} 

這裏的竅門在於computeCoin方法,並在事實上,分工整數除法,所以val將持有的給定值的硬幣的「最大」號(coinValue)其總值不超過amount

+1

一般來說,對於家庭作業來說,給出完整的解決方案被認爲是不好的形式。 –

+0

這些倒票真的只是爲了提供完整的解決方案嗎? – arshajii

+0

謝謝你們所有的時間,並幫助我對不起我的失望點@ A.R.S.i希望我能做點什麼 – pasito15

0

是什麼?

public class Assignment6 { 
    public static int amount; 
    public static int amountPreserv; 

    public static void main(String[] args) { 
     amount = (int) (Double.parseDouble(args[0]) * 100); 
       amountPreserv = amount; 

     System.out.println("Five: " + computeCoin(500)); 
     System.out.println("one: " + computeCoin(100)); 
     System.out.println("Q : " + computeCoin(25)); 
     System.out.println("D : " + computeCoin(10)); 
     System.out.println("N : " + computeCoin(5)); 
     System.out.println("P : " + computeCoin(1)); 
     System.out.println("Value inputed : " + amountPreserv); 
    } 

    private static int computeCoin(int i) { 
     int cont = 0; 
     while (amount > i) { 
      amount -= i; 
      cont++; 
     } 
     return cont; 
    } 
} 
+1

一般來說,對於作業來說,給出完整的解決方案被認爲是不好的形式。 –

+0

是真的。特別是在處理簡單問題時。我會更加關注。感謝@JohnB – ademar111190

+0

程序編譯和一切,但如果我給它一個值,我得到什麼作爲了說就是這五:0 之一:0 問:0 d:0 N:0 ,P:0 – pasito15

0

要做的最大的一點是做出改變是一個貪婪算法,這意味着在任何給定時間移動你最接近目標的選項是最有效的選擇。因此,對於任何數量和任何教派,最有效的算法應該是這樣的:

int total; 
int[] denom = { w, x, y, z }; 
int[] count = new int[denom.length] 
int i = 0; 
while (i < denom.length && total > 0) { 
    while (total >= denom[i]) 
    { 
    total -= denom[i]; 
    count[i]++; 
    } 
    i++ 
} 

編輯:任何面額是有點野心其實。只有你有一個最小面額才能保證每次都能做出改變。

+0

一般來說,對於作業來說,給出完整的解決方案被認爲是不好的形式 –