Java 只允许一个实例字段,但需要更多?

Java 只允许一个实例字段,但需要更多?,java,Java,我是一名学生,正在努力完成老师布置的某项实验任务。当我试图在Jcreator中编译时遇到问题,我得到一个错误,找不到符号。我认为这是因为我没有创造“美元”和“美分”。老师说学生只能有一个实例字段,那么我该如何解决这个问题呢 编辑:谢谢,我修复了模运算符并将返回值放入 我在“int$s=(int)total/PENNIES_PER_DOLLAR_VALUE;”行和“int cents=total%PENNIES_PER_DOLLAR_VALUE;”中遇到错误 谢谢 public class Co

我是一名学生,正在努力完成老师布置的某项实验任务。当我试图在Jcreator中编译时遇到问题,我得到一个错误,找不到符号。我认为这是因为我没有创造“美元”和“美分”。老师说学生只能有一个实例字段,那么我该如何解决这个问题呢

编辑:谢谢,我修复了模运算符并将返回值放入

我在“int$s=(int)total/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;
    }


 }
当您的
getDollars()
getCents()
方法声明返回
int
时,它们没有返回任何内容

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

您创建了一个名为PENNY_PER_DOLLAR_VALUE的常量:

但后来你提到的是每一美元的便士价值:


这是它找不到的符号。

从哪里得到错误?有一个持续的命名问题。请参阅下面我的更新答案。我还应该指出,
getCents()
方法中存在逻辑错误。我相信您希望使用模运算符(%)。
public static final int PENNY_PER_DOLLAR_VALUE = 100;
int dollars = (int) total / PENNIES_PER_DOLLAR_VALUE;
int cents = total % PENNIES_PER_DOLLAR_VALUE;