在java中计算未来值时,答案是错误的
因此,我正在创建一个未来价值应用程序,它允许用户输入他们已投资的金额、每年的百分比、投资的年数,以及是否按年度、半年、每月、每日等进行复利 我遇到的问题是,我的答案是错的,但只有几百个 根据我现在掌握的代码,我每半年使用一次示例:在java中计算未来值时,答案是错误的,java,Java,因此,我正在创建一个未来价值应用程序,它允许用户输入他们已投资的金额、每年的百分比、投资的年数,以及是否按年度、半年、每月、每日等进行复利 我遇到的问题是,我的答案是错的,但只有几百个 根据我现在掌握的代码,我每半年使用一次示例: PV = 1000 Interest Rate = 3 Years Invested = 10 Compounded = Semi Annually 我得到的答案是1806年 正确答案应该是1346.86 我的代码如下: public void action
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"))
{
}
}
}
谢谢你的帮助。如果您需要我进一步解释,请发表评论。您的问题与编码无关。。。这与你处理利率的方式有关
- 如果您每年复利,您每年有一个转换期
- 如果您每半年复利一次(或如您的问题所述的“半年复利”),您每年有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);
手工操作:
- 利率:3%
- 复合周期:2
- 年份:10
- 周期:2*10=20
- 有效利率:3%/2=1.5%
- 投资额:1000
- 未来价值:1000*(1+0.015)^20=1346.85500655
一个小礼物:更简洁的代码(因为我是个好人;) 你在完全复制你的代码!你可以清理它:
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);
}
}
为什么您的年度和月度计算具有相同的
compPeriod
的1
?是否应该是12
?请删除所有与UI相关的代码,并将问题隔离到不正确的特定功能。您是否尝试过调试应用程序?哪一种计算结果与您预期的不符(哪一种futureValue
)?还有,你期望什么价值(你得到了什么)?是的,我知道。对不起,我应该换一下。我在做实验。我现在就修。1.03^10=1.34391…
,它接近您得到的1346.86
。金融机构在数学方面做了一些奇怪的事情。我想是他击中了我的头。他们将“无息”利率除以复利利率,得到接近但不是真正的3%。根据您想要使用的公式是A=P(1+r/n)^nt
谢谢您的帮助!成功了@内森很乐意帮忙。如果你觉得这篇文章有用,请投票并/或接受它。。。如果你在学习金融数学或兴趣理论,你需要一本关于这个主题的好书