我是一位努力完成我的老師的特定實驗任務的學生。當我嘗試在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;
}
}
**哪裏有錯誤? – SLaks
有一個恆定的命名問題。看到我下面更新的答案。 –