等额本金还款法java实现
时间: 2024-01-01 09:22:16 浏览: 75
以下是Java实现等额本金还款法的代码:
```java
import java.math.BigDecimal;
public class AverageCapital {
public static void main(String[] args) {
BigDecimal totalLoan = new BigDecimal(100000); // 贷款总额
int months = 12; // 贷款期限(月)
BigDecimal annualInterestRate = new BigDecimal(0.05); // 贷款年利率
BigDecimal monthlyInterestRate = annualInterestRate.divide(new BigDecimal(12), 6, BigDecimal.ROUND_HALF_UP); // 贷款月利率
BigDecimal monthRepaymentCapital = totalLoan.divide(new BigDecimal(months), 2, BigDecimal.ROUND_HALF_UP); // 每月偿还本金
BigDecimal remainingLoan = totalLoan; // 剩余未还本金
BigDecimal totalInterest = new BigDecimal(0); // 总利息
for (int i = 1; i <= months; i++) {
BigDecimal monthInterest = remainingLoan.multiply(monthlyInterestRate).setScale(2, BigDecimal.ROUND_HALF_UP); // 每月偿还利息
BigDecimal monthRepayment = monthRepaymentCapital.add(monthInterest); // 每月偿还总额
remainingLoan = remainingLoan.subtract(monthRepaymentCapital); // 剩余未还本金
totalInterest = totalInterest.add(monthInterest); // 总利息
System.out.println("第" + i + "个月,偿还本金:" + monthRepaymentCapital + ",偿还利息:" + monthInterest + ",偿还总额:" + monthRepayment + ",剩余未还本金:" + remainingLoan);
}
System.out.println("总利息:" + totalInterest);
}
}
```
阅读全文