Educoder-练习-Java循环综合练习一之住房贷款还款计算

这题太恶心了,直接放代码,变量rate写的有点懵逼,估计也就我自己看得懂

import java.util.Scanner;

public class LoanTest{
	public static void main(String[] args){
			Scanner sc = new Scanner(System.in);
			double capital = sc.nextDouble();
			int year = sc.nextInt();

			System.out.println("贷款额:"+String.format("%.0f",capital));
			
			System.out.println("贷款年限:"+year);
			System.out.println("利率    月还款额    总还款额");
			
			// double rate = 0.05;	//利率
			
			double sum = 0;		//总还款额

			double month = 0; 	//月还款额
			for (double rate = 0.05; rate <= 0.08125; ){
				month = (capital * (rate / 12) * Math.pow(1 + rate / 12,year * 12)) /
						 (Math.pow(1 + rate / 12, year * 12) - 1); 
				
				sum = month * 12 * year;
				rate = rate * 100;
				System.out.println(String.format("%.3f",rate) + "%	"+String.format("%.2f",month) +"	"+String.format("%.2f",sum));
				rate = rate / 100;
				rate = rate + 0.00125;

				 
			}
			
			
			
		}
}
Java中,我们可以创建循环综合练习来模拟住房贷款还款计算的过程。假设我们有个简单的住房贷款模型,包括贷款总额、年利率、贷款期限(按月还款),以及等额本息(每月还款金额不变)或等额本金(每月偿还固定本金加剩余利息部分)两种还款方式。 首先,我们需要定义个函数接收这些参数: ```java public class LoanCalculator { public static double calculateMonthlyPayment(double loanAmount, double annualInterestRate, int loanTermInYears, boolean isAmortizationType) { // 程序逻辑... } } ``` 然后,你可以通过循环计算每个月的还款情况,例如等额本息: ```java for (int month = 1; month <= loanTermInYears * 12; month++) { double monthlyRate = annualInterestRate / 1200; // 将年利率转为月利率 double remainingBalance = loanAmount; double interestPart; if (isAmortizationType) { // 等额本息 interestPart = remainingBalance * monthlyRate; double monthlyPayment = monthlyInterest + remainingBalance; // 还款额 = 利息 + 本金余额 // 更新剩余余额 remainingBalance -= monthlyPayment; } else { // 等额本金 interestPart = remainingBalance * monthlyRate; double monthlyPayment = interestPart; // 逐月减少本金 remainingBalance -= monthlyPayment; } System.out.printf("第%02d个月,月供:%.2f元,本金:%.2f元,利息:%.2f元\n", month, monthlyPayment, monthlyPayment - interestPart, interestPart); } ``` 在这个例子中,循环会按照指定的贷款期限每月更新还款信息,并打印出来。至于
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值