2012-06-21 86 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

一般来说,对于作业来说,给出完整的解决方案被认为是不好的形式 –