java 模拟贷款实现等额本息还款
作者:互联网
还是菜鸟的我,在学习完java面向对象的思想后编写了一个模拟贷款的程序!!
一.首先创建一个类用于给变量赋值和输出。
代码如下:
public class Loan {
private double monthlyrate;//月利率
private int summonth; //贷款总月数
private double loanmount;//贷款本金
public Loan(double yearrate,double loans,int year){
this.monthlyrate=yearrate/(12*100);
this.summonth=year*12;
this.loanmount=loans;
}
public double getMonthlyPayment() {
return (loanmount*monthlyrate*Math.pow(1+monthlyrate,summonth))/(Math.pow(1+monthlyrate,summonth)-1);
}
public double getTotalPayment() {
return getMonthlyPayment()*summonth;
}
}
二.再在同一个包下新建一个类用于输出还款总额。
代码如下:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Loanmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
double yearrate;double loans ;
int year;
Scanner in =new Scanner (System.in);
System.out.println("请输入贷款年利率:");
yearrate=in.nextDouble();
System.out.println("请输入贷款年限:");
year=in.nextInt();
System.out.println("请输入贷款总金额");
loans=in.nextDouble();
Loan a=new Loan(yearrate,loans,year);
DecimalFormat df=new DecimalFormat("#.00");
System.out.println("等额本息的情况下每个月的还款额是"+a.getMonthlyPayment()+"元");
System.out.println("等额本息情况下总的还款额是"+df.format(a.getTotalPayment())+"元");
}
}
三.运行结果如下:
标签:等额,double,year,System,summonth,yearrate,本息,java,public 来源: https://blog.csdn.net/weixin_46351306/article/details/110943252