gpt4 book ai didi

java - 只允许一个实例字段但需要更多?

转载 作者:行者123 更新时间:2023-11-30 04:58:19 26 4
gpt4 key购买 nike

我是一名学生,正在努力完成老师布置的特定实验室作业。当我尝试在 Jcreator 中编译它时遇到问题,出现错误,找不到符号。我认为这是因为我没有创造“美元”和“美分”。老师说学生只能有一个实例字段,我该如何解决这个问题?

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

我在“int Dollars = (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;
}


}

最佳答案

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

public static final int PENNY_PER_DOLLAR_VALUE = 100;

但稍后您会提到 PENNIES_PER_DOLLAR_VALUE:

int dollars = (int) total / PENNIES_PER_DOLLAR_VALUE;

int cents = total % PENNIES_PER_DOLLAR_VALUE;

这是它找不到的符号。

关于java - 只允许一个实例字段但需要更多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7781630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com