Java : Comparing Loans
Problem Description:
Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8 using the following formula:
monthlyPayment = loanAmount * monthlyInterestRate /(1-(1/(1 + monthlyInterestRate))^numberOfYears*12)
Here is a sample run:
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13
5.25% 189.85 11391.59
...
7.875% 202.17 12129.97
8.0% 202.76 12165.83
代码:
import java.math.BigDecimal;
import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("贷款金额:");
double in=input.nextDouble();
System.out.print("年限:");
double y=input.nextDouble();
System.out.println("利率 月供 总付");
double a=5;
double mp,ap;
do{
BigDecimal value = new BigDecimal(a);
BigDecimal ans= value.stripTrailingZeros();
mp=(in*a/100/12)/(1-(1/Math.pow(1+a/100/12,y*12)));
ap=mp*y*12;
System.out.print(ans+"%\t");
System.out.printf(" %.2f %.2f \n",mp,ap);
a+=0.125;
}while(a<=8);
input.close();
}
}