gpt4 book ai didi

java - Java 中使用 BigDecimal 的二维数组

转载 作者:行者123 更新时间:2023-12-01 12:20:22 25 4
gpt4 key购买 nike

我收到编译器错误,这些错误似乎指向两个语句末尾的分号,并且我不知道如何解决该问题。目标是从 Java 中的枚举类获取值,并将它们用于二维数组的财务计算。

public enum User
{
Mike("Mike", "User", 8000, 0.03, 15, 25),
Jim("Jim", "User", 12000, 0.05, 12, 30),
Jane("Jane", "User", 9000, 0.02, 18, 25),
Mary("Mary", "User", 18000, 0.09, 5, 30);
Bill("Bill", "User", 14000, 0.06, 8, 30);


private final String firstName;
private final String lastName;
private final int annualDeposit;
private final double annualIntRate;
private final int yearsToDeposit;
private final int yearsToCalcInt;


User(String firstName, String lastName, int annualDeposit, double annualIntRate,
int yearsToDeposit, int yearsToCalcInt)
{
this.firstName = firstName;
this.lastName = lastName;
this.annualDeposit = annualDeposit;
this.annualIntRate = annualIntRate;
this.yearsToDeposit = yearsToDeposit;
this.yearsToCalcInt = yearsToCalcInt;
}

public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}

public int getAnnualDeposit()
{
return annualDeposit;
}

public double getAnnualIntRate()
{
return annualIntRate;
}

public int getYearsToDeposit()
{
return yearsToDeposit;
}

public int getYearsToCalcInt()
{
return yearsToCalcInt;
}

}
<小时/>
import java.util.EnumSet;
import java.math.BigDecimal;
import java.text.NumberFormat;

public class Work
{
public static void main(String[] args)
{

int[] annualDeposit = new int[5];
double[] intRate = new double[5];
int[] yearsToDeposit = new int[5];
int[] yearsToCalcInt = new int[5];

BigDecimal[][] accountBalance = new BigDecimal[5][30];
BigDecimal[][] principal = new BigDecimal[5][30];

int count = 0;
int yearsDeposit = 0;

for (User user: User.values())
{
annualDeposit[count] = user.getAnnualDeposit();
intRate[count] = user.getAnnualIntRate();
yearsToDeposit[count] = user.getYearsToDeposit();
yearsToCalcInt[count] = user.getYearsToCalcInt();
count++;
}

for (int i = 0; i < 5; i++)
{
yearsDeposit = 0;
for (int year = 0; year <= yearsToCalcInt[i]; year++)
{
if(yearsDeposit <= yearsToDeposit[i])
{
principal[i][year] += annualDeposit[i] + accountBalance[i][year];
BigDecimal accountBalance[i][year] =
principal[i][year].multiply(intRate[i].add(BigDecimal.ONE).pow(year));

yearsDeposit++;
}
else
{
principal[i][year] += accountBalance[i][year];
BigDecimal accountBalance[i][year] =
principal[i][year].multiply(intRate[i].add(BigDecimal.ONE).pow(year));
}
}
}


}
}

最佳答案

Mary; 结尾,Java 认为这意味着 enum 不再有条目...

public enum User {

Mike("Mike", "User", 8000, 0.03, 15, 25),
Jim("Jim", "User", 12000, 0.05, 12, 30),
Jane("Jane", "User", 9000, 0.02, 18, 25),
Mary("Mary", "User", 18000, 0.09, 5, 30); <--- Culprit here...
Bill("Bill", "User", 14000, 0.06, 8, 30);

改为,...

public enum User {

Mike("Mike", "User", 8000, 0.03, 15, 25),
Jim("Jim", "User", 12000, 0.05, 12, 30),
Jane("Jane", "User", 9000, 0.02, 18, 25),
Mary("Mary", "User", 18000, 0.09, 5, 30),
Bill("Bill", "User", 14000, 0.06, 8, 30);

Nit Pick:约定规定枚举文字应为大写

public enum User {

MIKE("Mike", "User", 8000, 0.03, 15, 25),
JIM("Jim", "User", 12000, 0.05, 12, 30),
JANE("Jane", "User", 9000, 0.02, 18, 25),
MARY("Mary", "User", 18000, 0.09, 5, 30),
BILL("Bill", "User", 14000, 0.06, 8, 30);

参见Code Conventions for the Java TM Programming Language了解更多详情

关于java - Java 中使用 BigDecimal 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706400/

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