gpt4 book ai didi

java - java中的递归代码用于计算银行的资金账户

转载 作者:行者123 更新时间:2023-11-30 07:31:28 24 4
gpt4 key购买 nike

我想用java实现一个递归代码,用于计算投资几年后银行的资金账户......这是我的代码

public static double computeCapital(double capital, int years, double interestRate) {
if (years == 0) {
return capital;
} else {
double newcapital = capital * Math.pow(interestRate,year);
return computeCapital(newcapital , years+1 , interestRate);
}
}

我的代码正确吗?谢谢

最佳答案

   public static double computeCapital(double capital, int years, double interestRate) {
if (years == 0) {
return capital;
}
else{
return computeCapital(capital, years-1, interestRate)*(1+interestRate);
}
}

关于java - java中的递归代码用于计算银行的资金账户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056266/

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