所以我正在创建一个未来的价值应用程序,允许用户输入他们投资的金额,每年的百分比,投资的年数,以及是否每年,半年,每月,每日复合等等。
我遇到的问题是我的答案是错误的,但只有几百个。
使用我现在的代码,我使用半年的例子:
PV = 1000
Interest Rate = 3
Years Invested = 10
Compounded = Semi Annually
我得到的答案是1806.
正确答案是1346.86,据说是。
我的代码在这里:
public void actionPerformed(ActionEvent event)
{
String input1 = textInitialAmount.getText();
String input2 = textAnnualInterest.getText();
String input3 = textNumberOfYears.getText();
String comp = textCompounded.getText();
double invest = Double.parseDouble(input1);
double interest = Double.parseDouble(input2);
double numberofyear = Double.parseDouble(input3);
if(comp.equals("Annually"))
{
double compPeriod = 1;
double compNumberOfYear = numberofyear * compPeriod;
double rate = (interest / 100) + 1;
double valueFuture = invest * Math.pow(rate, compNumberOfYear);
String output = String.format("%5.0f", valueFuture);
futureValue.setText("The future value is " + output);
}
if (comp.equals("Semi-Annually"))
{
double compPeriod = 2;
double compNumberOfYear = compPeriod * numberofyear;
double percentRate = interest / 100;
double rate = percentRate + 1;
double valueFuture = invest * Math.pow(rate, compNumberOfYear);
String output = String.format("%5.0f", valueFuture);
futureValue.setText("The future value is " + output);
}
if(comp.equals("Monthly"))
{
double compPeriod = 12;
double compNumberOfYear = compPeriod * numberofyear;
double rate = 1 + (interest / 100);
double valueFuture = invest * Math.pow(rate, compNumberOfYear);
String output = String.format("%5.0f", valueFuture);
futureValue.setText("The future value is " + output);
}
if(comp.equals("Bi-Weekly"))
{
}
}
}
感谢您的帮助。如果您需要我解释更多,请发表评论。
答案 0 :(得分:2)
您的问题与编码无关......这与您处理利率的方式有关!
正确的代码是这样的:
double compPeriod = 2;
double compNumberOfYear = compPeriod * numberOfYear;
double percentRate = (interest / compPeriod) / 100;
// ^^^^^^^^^^^^^^^^^^^^^
// You missed this!
double rate = percentRate + 1;
double valueFuture = invest * Math.pow(rate, compNumberOfYear);
手工完成:
一个小礼物:更清洁,更简单的代码 (因为我是一个好人;))
你正在复制你的代码!你可以清理它:
public void actionPerformed(ActionEvent event) {
String input1 = textInitialAmount.getText();
String input2 = textAnnualInterest.getText();
String input3 = textNumberOfYears.getText();
String comp = textCompounded.getText();
double invest = Double.parseDouble(input1);
double interest = Double.parseDouble(input2);
double numberofyear = Double.parseDouble(input3);
// You just need to declare variables once!
double compPeriod;
switch(comp.toLowerCase()) {
case "annually":
compPeriod = 1;
break;
case "semiannually":
compPeriod = 2;
break;
case "monthly":
compPeriod = 12;
break;
case "bi-weekly":
compPeriod = 26; // Assuming 52 weeks per year
break;
case "weekly":
compPeriod = 52; // Assuming 52 weeks per year
break;
default:
compPeriod = 1;
}
double compNumberOfYear = numberofyear * compPeriod;
double valueFuture = invest * Math.pow(1 + (interest / compPeriod) / 100, compNumberOfYear);
String output = String.format("%5.0f", valueFuture);
futureValue.setText("The future value is " + output);
}
}