package cn.hncu.Chapter2;
import javax.swing.JOptionPane;
/**
* @author hncu_chx
*
* Mylove amin
*
*/
public class ComputeLoan {
public static void main(String[] args) {
//Enter yearly interest rate开始的年利率
String annualInterestRateString = JOptionPane.showInputDialog("Enter yearly " +
"interest rate,for example 8.25:");
//Convert转换… string to double
double annualInterestRate =
Double.parseDouble(annualInterestRateString);
//Obtain获得 monthly每月的 interest rate利息率
double monthlyInterestRate = annualInterestRate/1200;
//Enter number of years
String numberOfYearsString =JOptionPane.showInputDialog("Enter number of years as an integer," +
"\nfor example 5:");
//Convert转换 string to int
int numberOfYears = Integer.parseInt(numberOfYearsString);
//Enter开始 loan借款 amount总额
String loanString = JOptionPane.showInputDialog("Enter loan amount," +
"for example 120000.95:");
//Convert string to double
double loanAmount = Double.parseDouble(loanString);
//Calculate 计算 payment付款,支付;报酬
double monthlyPayment = loanAmount * monthlyInterestRate/(1-1/Math.pow(1+monthlyInterestRate,
numberOfYears*12));
double totalPayment = monthlyPayment*numberOfYears*12;
//Format格式 to keep tow digits位数 数字 after the decimal小数 point点
//格式保留小数点后两位数
monthlyPayment = (int )(monthlyPayment *100)/100.0;
totalPayment = (int)(totalPayment *100)/100.0;
//Display显示 results结果 成绩
String output = "The monthly payment is "+monthlyPayment+
"\nThe total payment is "+totalPayment;
JOptionPane.showMessageDialog(null, output);
}
}