2011-10-16 37 views
1

我是一位努力完成我的老師的特定實驗任務的學生。當我嘗試在Jcreator中編譯此問題時出現問題,出現錯誤,無法找到符號。我認爲這是由於我沒有創造「美元」和「美分」的事實。老師說學生只能有一個實例字段,所以我該如何解決這個問題?只允許一個實例字段但需要更多?

編輯:謝謝,我固定的模運算,並把返回值

我得到的錯誤「INT美元=(int)的總/ PENNIES_PER_DOLLAR_VALUE;」行和「int cents = total%PENNIES_PER_DOLLAR_VALUE;」。

感謝

public class CoinCounter 
{ 
    // constants 
    //*** These are class constants so they need public static 
    public static final int QUARTER_VALUE = 25; 
    public static final int DIME_VALUE = 10; 
    public static final int NICKEL_VALUE = 5; 
    public static final int PENNY_VALUE = 1; 
    public static final int PENNY_PER_DOLLAR_VALUE = 100; 

    // instance field (one - holds the total number of cents EX: 8,534) 
    private int total; 

    /** 
    * Constructs a CoinCounter object with a specified number of pennies, 
    * nickels, dimes and quarters 
    * @param quarterAmount the amount of quarters 
    * @param dimeAmount the amount of dimes 
    * @param nickelAmount the amount of nickels 
    * @param pennyAmount the amount of pennies 
    */ 
    public CoinCounter(int quarters, int dimes, int nickels, int pennies) 
    { 
     total = quarters * QUARTER_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + pennies; 

    } 
    // add remaining methods as described 

    /** 
    * getDollars returns the number of dollars in the CoinCounter 
    * @return the number of dollars 
    */ 
    public int getDollars() 
    { 
     int dollars = (int) total/PENNIES_PER_DOLLAR_VALUE; 
      return dollars; 
    } 
    /** 
    * getCents returns the number the numbers of cents left over after the dollars are removed 
    * @return the number of cents left over 
    */ 
    public int getCents() 
    { 
     int cents = total % PENNIES_PER_DOLLAR_VALUE; 
      return cents; 
    } 


} 
+1

**哪裏有錯誤? – SLaks

+0

有一個恆定的命名問題。看到我下面更新的答案。 –

回答

1

您創建了一個常量名爲PENNY_PER_DOLLAR_VALUE:

public static final int PENNY_PER_DOLLAR_VALUE = 100; 

但是後來你參考以PENNI ES_PER_DOLLAR_VALUE:

int dollars = (int) total/PENNIES_PER_DOLLAR_VALUE; 

int cents = total % PENNIES_PER_DOLLAR_VALUE; 

這就是它無法找到象徵。

1

,當他們宣佈,他們返回int的你getDollars()getCents()方法不返回任何東西。

public int getDollars() 
{ 
    int dollars = (int) total/PENNIES_PER_DOLLAR_VALUE; 
    return dollars; 
} 

public int getCents() 
{ 
    int cents = total % PENNIES_PER_DOLLAR_VALUE; 
    return cents; 
} 

編輯:

的問題是你的常量的命名。

您定義的:

public static final int PENNY_PER_DOLLAR_VALUE = 100; 

但是你用這個:

PENNIES_PER_DOLLAR_VALUE 
+0

我也應該指出你的'getCents()'方法有一個邏輯錯誤。我相信你想使用模運算符(%)。 –

+0

我已經更新了我的答案。 –

相關問題