import java.text.NumberFormat;
import java.util.Scanner;
public class Repay {
final double NLL=0.0749; //年利率
final double MLL=NLL/12; //月利率
final int MONTH=12; //付款次數
int month=1;
public static void main(String[] args){
Repay rp=new Repay();
rp.payback();
}
public void payback(){
System.out.println("請輸入借款金額");
//獲得貸款數額
Scanner sc=new Scanner(System.in);
double debt=sc.nextDouble();
NumberFormat fn=NumberFormat.getInstance();
fn.setMaximumFractionDigits(2);
String nll=fn.format(NLL*100)+"%";
String mll=fn.format(MLL*100)+"%";
String debt_fn=fn.format(debt);
System.out.println("請選擇還款方式:輸入1選擇等額本金還款,輸入2選擇等額本息還款");
int mode=sc.nextInt();
//等額本金還款
if(mode==1){
System.out.println("您總***借款"+debt_fn+";還款方式:等額本金還款;還款時間:1年"+";年利率是:"+nll+";月利率"+mll);
System.out.println("分期還款明細");
double monthPincipal=debt/12; //每月應還本金
debt=monthPincipal*12;
double accrualM; //每月還款利息
double tm; //每月還款金額
//分期還款明細
while(debt>=1){
accrualM=debt*MLL;
tm=monthPincipal+accrualM;
debt=debt-monthPincipal;
if(debt<1){
debt=0;
}
//把小數位數格式化成2位
String tm_fn=fn.format(tm);
String monthPincipal_fn=fn.format(monthPincipal);
String accrualM_fn=fn.format(accrualM);
String debt_fn2=fn.format(debt);
System.out.println("第"+month+"月 還款金額:"+tm_fn+" 本月應還本金:"+monthPincipal_fn+" 本月還款利息:"+accrualM_fn+" 剩余本金:"+debt_fn2);
month++;
}
}
//等額本息還款
if(mode==2){
System.out.println("您總***借款"+debt_fn+";還款方式:等額本息還款;還款時間:1年"+";年利率是:"+nll+";月利率"+mll);
//等額本息還款的月還款數公式
double X=debt*MLL*(Math.pow((1+MLL), MONTH))/(Math.pow((1+MLL), MONTH)-1);
String X_fn=fn.format(X); //格式化小數位數
System.out.println("您的月還款額為:"+X_fn);
//分期還款明細
double lixiM,benjinM; //月利息,月本金
System.out.println("分期還款明細");
while(debt>=1){
lixiM=debt*MLL;
benjinM=X-lixiM;
debt=debt-benjinM;
if(debt<1){
debt=0;
}
//輸出
String lixiM_fn=fn.format(lixiM);
String benjinM_fn=fn.format(benjinM);
String debt_fn3=fn.format(debt);
System.out.println("第"+month+"月 還款金額:"+X_fn+" 本月應還本金(即減少債務的錢):"+benjinM_fn+" 本月還款利息:"+lixiM_fn+" 剩余本金:"+debt_fn3);
month++;
}
}
}
}