java - 用java计算利率

标签 java for-loop rate

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

    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/

相关文章:

java - 资源管理器崩溃后恢复 Java 系统托盘图标

java - 为什么下面的QuickSortAlgo方法中使用的是修改后的double[]变量 'arr'而不是我们最初输入的arr[]?

java - Android 将其打印到左侧?

python - 如何在python中的子流程中使用错误处理

ffmpeg CLI 设置 -r 30 如果输入 => 30 fps,否则 -r 如果 < 30 fps 则采用输入 fps

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

javascript - 如何在 HTML 中的每个项目之间延迟打印列表

javascript - 如何将路径文件字符串拆分为不同的部分,然后使用 JavaScript 将其打印出来?

android - 加速度计传感器速率可以信任吗?

c# - FedEx express 服务