java写一个房贷计算器
时间: 2023-04-07 10:01:55 浏览: 497
好的,我可以回答这个问题。房贷计算器可以通过输入贷款金额、贷款年限、利率等信息,计算出每月还款额、还款总额、利息总额等信息。以下是一个简单的 Java 代码示例:
```
import java.util.Scanner;
public class MortgageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入贷款金额(万元):");
double amount = scanner.nextDouble() * 10000;
System.out.print("请输入贷款年限(年):");
int years = scanner.nextInt();
System.out.print("请输入贷款利率(%):");
double rate = scanner.nextDouble() / 100;
int months = years * 12;
double monthlyRate = rate / 12;
double monthlyPayment = amount * monthlyRate * Math.pow(1 + monthlyRate, months) / (Math.pow(1 + monthlyRate, months) - 1);
double totalPayment = monthlyPayment * months;
double totalInterest = totalPayment - amount;
System.out.println("每月还款额:" + monthlyPayment);
System.out.println("还款总额:" + totalPayment);
System.out.println("利息总额:" + totalInterest);
}
}
```
这个程序会提示用户输入贷款金额、贷款年限和利率,然后计算出每月还款额、还款总额和利息总额,并输出结果。
阅读全文