gpt4 book ai didi

java - 用java计算利率

转载 作者:行者123 更新时间:2023-12-02 04:20:07 24 4
gpt4 key购买 nike

我正在制作一种方法,该方法应该计算特定年份内一定金额的利率(这些值已在参数中定义)。这是我到目前为止的代码:

    public void balance(int amount, double rate, int year){
double yearlyRate = amount * rate;
double totalAmount;


System.out.print(amount + " :- " + " grows with the interest rate of " + rate);

for (int i = 0; i <= year; i++ ){

for ( int j = amount; j)
totalAmount = amount + yearlyRate;
System.out.println(i + " " + totalAmount);
}

}

我正在制作嵌套 for 循环,正如您所看到的,那里缺少代码。在这里我遇到了一些麻烦。第一个 for 循环遍历年份,另一个 for 循环应该计算总量。为了清楚变量“intyear”中定义的年份,假设它是7,那么程序应该计算每年金额的增长,如下所示:

year1 totalAmount 
year2 totalAmount
year3 totalAmount
and so on.....

主要方法如下所示:

public void exerciceG(Prog1 prog1) {
System.out.println("TEST OF: balance");
prog1.balance(1000, 0.04, 7);
}

非常感谢我能得到的任何帮助!

最佳答案

这里需要进行一项更改,但正如我在评论中提到的,可能还有很多其他事情要做:

totalAmount = totalAmount + amount + yearlyRate;

可以写成:

totalAmount += amount + yearlyRate;

您可能还想删除 for j 循环,因为它不会按原样执行任何操作。

编辑

这是一个猜测,因为我不确定我们是否知道目标是什么,但是怎么样:

public static void balance(int amount, double rate, int year){

double yearlyInterestPaid ;
double totalAmount = amount;


System.out.println(amount + " :- " + " grows with the interest rate of " + rate);

for (int i = 0; i <= year; i++ ){

yearlyInterestPaid = totalAmount * rate;
totalAmount += yearlyInterestPaid;
System.out.println(i + " " + totalAmount);
}
}

这是输出:

TEST OF: balance
1000 :- grows with the interest rate of 0.04
0 1040.0
1 1081.6
2 1124.8639999999998
3 1169.85856
4 1216.6529024
5 1265.319018496
6 1315.93177923584
7 1368.5690504052736

可以合理地假设这就是目标。

关于java - 用java计算利率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875448/

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