实验一数据类型--Java

计算并输出贷款的月支付金额

1>我交的,当时刚开始学

public class HHH {
    
    
public static void main(String[] args) {
    
    
  // TODO 自动生成的方法存根
       float sum=1000000;
       double month=0.2,year=4.0;
       double  yuelixi=(sum*month)/(1-1/Math.pow(1+month,year*12));
    System.out.println("月支付利息为:"+yuelixi);
 }
}

2>参考答案

import java.util.Scanner;
import java.text.DecimalFormat;
public class E1 {
    
    
/** 
* 获得每月支付金额 
* * @param totalMoney  贷款总额 
* * @param year   贷款年限 
* * @param lilvPerMonth   月利率 
* */
public double getMoneyPerMonth(int totalMoney,int year,double lilvPerMonth)    
{
    
        
double dbMoney=(totalMoney*lilvPerMonth)/(1-(1/Math.pow((1+lilvPerMonth),year*12)));    
return dbMoney;    
}
public static void main(String[] args) {
    
    
int totalMoney,year;
double lilvPerMonth;
Scanner scanner =new Scanner(System.in);//从键盘输入贷款总额、贷款年限、月利率
System.out.print("请输入贷款总额:");
totalMoney=scanner.nextInt();
System.out.print("请输入贷款年限:");
year=scanner.nextInt();
System.out.print("请输入贷款月利率(参考值:0.003-0.006):");
lilvPerMonth=scanner.nextDouble();E1 e1=new E1();
double dbMoneyPerMonth=e1.getMoneyPerMonth(totalMoney, year, lilvPerMonth);
//格式化数字
DecimalFormat df1 = new DecimalFormat("0.00");
System.out.println("dbMoneyPerMonth" + df1.format(dbMoneyPerMonth));
}
}

猜你喜欢

转载自blog.csdn.net/weixin_45800653/article/details/107955007