模拟贷款系统,根据输入的贷款年利率、贷款年限和货款总额,计算等额本息情况下的月均还款额和总还款额并输出。
时间: 2024-10-16 15:05:54 浏览: 39
(2021-2022)年专题资料公积金贷款月还款额查询表等额本息.doc
模拟贷款系统通常用于演示金融计算器的功能,其中包含计算每月偿还本金和利息的过程。等额本息还款法(也称为固定月供)是最常见的贷款还款方式之一,每个月偿还金额相等。下面是使用Java编写的简单示例,计算等额本息还款的月均还款额和总还款额:
```java
import java.util.Scanner;
public class LoanCalculator {
private static final double MONTHS_IN_YEAR = 12;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入贷款总额:");
double loanAmount = scanner.nextDouble();
System.out.println("请输入贷款年利率(小数形式):");
double annualInterestRate = scanner.nextDouble() / 100; // 将百分比转换为小数
System.out.println("请输入贷款年限(年):");
int loanTermYears = scanner.nextInt();
double totalPayments = calculateTotalPayments(loanAmount, annualInterestRate, loanTermYears * MONTHS_IN_YEAR);
double monthlyPayment = calculateMonthlyPayment(loanAmount, annualInterestRate, loanTermYears * MONTHS_IN_YEAR);
System.out.printf("每月还款额: %.2f\n", monthlyPayment);
System.out.printf("总还款额: %.2f\n", totalPayments);
}
// 计算总还款额
private static double calculateTotalPayments(double loanAmount, double interestRate, int loanMonths) {
return loanAmount * (interestRate * Math.pow((1 + interestRate), loanMonths)) / ((Math.pow((1 + interestRate), loanMonths)) - 1);
}
// 计算每月还款额
private static double calculateMonthlyPayment(double loanAmount, double interestRate, int loanMonths) {
double monthlyInterestRate = interestRate / MONTHS_IN_YEAR;
return (loanAmount * monthlyInterestRate) / (1 - Math.pow((1 - monthlyInterestRate), loanMonths));
}
}
```
在这个程序中,`calculateTotalPayments`函数使用了贷款的本金、年利率和总月份数来计算总还款额,而`calculateMonthlyPayment`函数则利用月利率和贷款期数来计算每月应还金额。
阅读全文