Java写等额本息算法
时间: 2023-05-22 19:05:27 浏览: 229
非常感谢您的问题!下面是Java写等额本息算法的示例代码:
```
public static void main(String[] args) {
double amount = 100000; // 贷款总额
double annualRate = 0.08; // 年利率
int periods = 12; // 还款期数(月)
// 计算每期还款金额
double monthlyRate = annualRate / 12; // 月利率
double monthlyPayment = amount * monthlyRate * Math.pow((monthlyRate + 1), periods) / (Math.pow((monthlyRate + 1), periods) - 1);
// 计算总利息
double totalInterest = 0;
double balance = amount;
for (int i = 1; i <= periods; i++) {
double interest = balance * monthlyRate;
double principal = monthlyPayment - interest;
balance -= principal;
totalInterest += interest;
System.out.println(String.format("第%d期:还款金额 %.2f,本金 %.2f,利息 %.2f,剩余贷款 %.2f", i, monthlyPayment, principal, interest, balance));
}
System.out.println(String.format("贷款总额 %.2f,总利息 %.2f", amount, totalInterest));
}
```
希望这段代码可以帮助到您!
阅读全文