我不确定枚举 Family 是否处于正确的位置,但是无论它在我的主类内部还是外部,我都会遇到相同的错误。这些枚举; WILMA、FRED、BETTY 和 BARNEY 将提供 数据来计算他们的退休储蓄。
public class Assignment5
{
enum Family
{
WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35),
FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty
BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25),
BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35),
}
//The properties’ “getters”/accessors should go here.
//instance fields
private final String firstName; //first names
private final String lastName; //last names
private final int annualDeposit; //annual deposit
private final double annualRate; //annual rate
private final int yearsDeposit; //number of years depositing
private final int yearsCalc; //number of years to compound
//enum family constructor
Assignment5(String firstName, String lastName, int annualDeposit, double annualRate, int yearsDeposit, int yearsCalc)
{
this.firstName = firstName;
this.lastName = lastName;
this.annualDeposit = annualDeposit;
this.annualRate = annualRate;
this.yearsDeposit = yearsDeposit;
this.yearsCalc = yearsCalc;
}
//getter for firstName
public String getFirstName()
{
return firstName;
}
//getter for lastName
public String getLastName()
{
return lastName;
}
//getter for annual deposit
public int getAnnualDeposit()
{
return annualDeposit;
}
//getter for annual rate
public double getAnnualRate()
{
return annualRate;
}
//getter for years deposit
public int getYearsDeposit()
{
return yearsDeposit;
}
//getter for years calc
public int getYearsCalc()
{
return yearsCalc;
}
//main method
//formulas for calculating retirement account and printing results will go here
public static void main(String[] args)
{
}
}
最佳答案
要创建带有参数的枚举
,您应该使用合适的构造函数。根据您的情况,将枚举更改为:
enum Family
{
WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35),
FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty
BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25),
BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35);
//instance fields
private final String firstName; //first names
private final String lastName; //last names
private final int annualDeposit; //annual deposit
private final double annualRate; //annual rate
private final int yearsDeposit; //number of years depositing
private final int yearsCalc; //number of years to compound
Family(String firstName, String lastName, int annualDeposit, double annualRate, int yearsDeposit, int yearsCalc)
{
this.firstName = firstName;
this.lastName = lastName;
this.annualDeposit = annualDeposit;
this.annualRate = annualRate;
this.yearsDeposit = yearsDeposit;
this.yearsCalc = yearsCalc;
}
}
关于java - Java 中枚举构造函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26615579/