Java 如何将这个复合值函数转换为for循环?
好吧,我是Java新手。我有一个程序,在向用户询问储蓄金额和年利率后计算复合值。我很难让它进入for循环,但我觉得我有点接近了?我脑子里最困难的部分是理解如何把最后几个月的总数计算进新的计算中 下面是我目前计算复合值的公式:Java 如何将这个复合值函数转换为for循环?,java,loops,for-loop,Java,Loops,For Loop,好吧,我是Java新手。我有一个程序,在向用户询问储蓄金额和年利率后计算复合值。我很难让它进入for循环,但我觉得我有点接近了?我脑子里最困难的部分是理解如何把最后几个月的总数计算进新的计算中 下面是我目前计算复合值的公式: firstMonth = savingAmount * (1 + monthlyInterestRate); secondMonth = (savingAmount + firstMonth) * (1 + monthlyInterestRate);
firstMonth = savingAmount * (1 + monthlyInterestRate);
secondMonth = (savingAmount + firstMonth) * (1 + monthlyInterestRate);
thirdMonth = (savingAmount + secondMonth) * (1 + monthlyInterestRate);
fourthMonth = (savingAmount + thirdMonth) * (1 + monthlyInterestRate);
fifthMonth = (savingAmount + fourthMonth) * (1 + monthlyInterestRate);
sixthMonth = (savingAmount + fifthMonth) * (1 + monthlyInterestRate);
这显然是丑陋的,它应该在一个循环。同样,savingAmount是用户输入,annualInterestate是用户输入。月度利率为年度利率/12
这是我到目前为止的for循环
for (int i = 1; i <= 6; i++ );
{
sixthMonth = savingAmount * Math.pow(1+monthlyInterestRate, 6);
}
for(inti=1;i试试这个:
for (int i = 1; i <= 6; i++) {
monthAmount = (savingAmount + monthAmount) * (1 + monthlyInterestRate);
}
for(inti=1;i试试这个:
for (int i = 1; i <= 6; i++) {
monthAmount = (savingAmount + monthAmount) * (1 + monthlyInterestRate);
}
for(int i=1;i如果您想知道每月可以节省多少钱,我建议您将金额存储在一个列表中以便于查找
public static void main(String args[]) {
double savingsAmount = 543.23;
double annualInterestRate = 0.85; // %
double monthlyInterestRate = annualInterestRate / 12;
List<Double> savings = new ArrayList<Double>();
savings.add(savingsAmount); // month 0
int monthsInTheFuture = 6;
double compoundInterest = 1 + monthlyInterestRate;
for (int i = 1; i <= monthsInTheFuture; i++) {
double previousSavings = savings.get(i-1);
double nextSavings = previousSavings * compoundInterest;
savings.add(nextSavings);
}
System.out.println(savings);
}
publicstaticvoidmain(字符串参数[]){
双重储蓄金额=543.23;
双年度利率=0.85;//%
双月利率=年利率/12;
列表节省=新建ArrayList();
savings.add(savingsAmount);//第0个月
int monthsInTheFuture=6;
双成分兴趣=1+月兴趣;
对于(int i=1;i我建议将金额存储在一个列表中,以便于查找,如果您想查看每个月可以节省多少钱的话
public static void main(String args[]) {
double savingsAmount = 543.23;
double annualInterestRate = 0.85; // %
double monthlyInterestRate = annualInterestRate / 12;
List<Double> savings = new ArrayList<Double>();
savings.add(savingsAmount); // month 0
int monthsInTheFuture = 6;
double compoundInterest = 1 + monthlyInterestRate;
for (int i = 1; i <= monthsInTheFuture; i++) {
double previousSavings = savings.get(i-1);
double nextSavings = previousSavings * compoundInterest;
savings.add(nextSavings);
}
System.out.println(savings);
}
publicstaticvoidmain(字符串参数[]){
双重储蓄金额=543.23;
双年度利率=0.85;//%
双月利率=年利率/12;
列表节省=新建ArrayList();
savings.add(savingsAmount);//第0个月
int monthsInTheFuture=6;
双成分兴趣=1+月兴趣;
对于(inti=1;i我实际上建议使用复利公式A=p(1+r/n)^nt
- A-复合值
- P-本金投资
- r-年利率
- n-每年复利的次数
- t年复合
因此,您可以将所有这些简化为:
compoundValue = savingAmount * Math.pow(1 + monthlyInterestRate/12,
monthlyInterestRate * yearsCompounded);
实际上,我建议使用复利公式A=p(1+r/n)^nt
- A-复合值
- P-本金投资
- r-年利率
- n-每年复利的次数
- t年复合
因此,您可以将所有这些简化为:
compoundValue = savingAmount * Math.pow(1 + monthlyInterestRate/12,
monthlyInterestRate * yearsCompounded);
为什么要使用Math.pow?你在那行说(1+interest)^6为什么不让你的循环以与上面代码相同的方式工作?你的for
循环有一个尾随分号(;
)。意思是下一个块,{…}
,将只执行一次。一旦解决了这个问题,你应该检查你的逻辑。你有效地将第六个月的设置为相同的6次。你为什么要使用Math.pow?你说的是(1+兴趣)^6在那一行上,为什么不让你的循环与上面的代码一样工作呢?你的for
循环后面有一个分号(;
)。意思是下一个块,{…}
,将只执行一次。一旦修复,您应该检查您的逻辑。您的有效设置是将第六个月的设置为相同的6次。@JonnyHenly-这是一个列表,而不是一个Array@JonnyHenly-这是一个列表,不是一个数组。我认为这是正确的方法+1,但我也认为OP正在努力提高他们的理解我认为这是正确的方法+1,但我也认为OP正试图更好地理解for
循环。这可能是一个愚蠢的问题,但monthAmount从何而来?是否设置为双精度?monthAmount变量基本上替换了第六个月的变量。这个变量是双精度变量,将t在循环结束时给出你的最终数量。哇,我觉得自己太愚蠢了。我这么做了,但我忘了将第六个月的变量之一更改为monthAmount。非常感谢你帮我得到它!!@josh102894没问题,很乐意帮助。别忘了选择解决你问题的答案,它可以在以后帮助你处境中的其他人。这可能是一个愚蠢的问题,但是monthAmount是从哪里来的?这是一个双精度变量吗?monthAmount变量基本上取代了你的第六个月变量。这个变量是双精度变量,在循环结束时会输出你的最终数量。哇,我感到难以置信的愚蠢。我这么做了,我忘了将第六个月的变量之一更改为monthAmount。谢谢你你帮了我这么多!!@josh102894没问题,很乐意帮忙。别忘了选择解决你问题的答案,它可以在以后帮助你处境中的其他人。