當前位置:外匯行情大全網 - 助學貸款 - 求寫壹個Java小程序~~

求寫壹個Java小程序~~

截圖:題目翻譯過來的大概意思和程序代碼:譯文:編寫壹個程序,使之能顯示同每月按揭貸款還款額以及欠款余額,然後顯示還款中有多少是利息還款,有多少是本金還款(即有多少還款是真正用來減少債務的)。假設年利率是7.49%。命名壹個常量來代表利率。註意還款按月進行,所以利率只是年利率7.49的1/12。 代碼:註:按揭貸款有兩種月供還款方式:本金還款和本息還款,題目要求的是按“本息還款”方式進行編程,再程序中我把兩種還款方式都寫了出來,關鍵地方有註釋!

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++;

}

}

}

}

  • 上一篇:請幫我看看2007年的運勢。本人三月十點出生於河北定州,農歷1976,男,現居杭州。
  • 下一篇:人家別人全款買的房子想在我想買他的房子怎麽辦按揭
  • copyright 2024外匯行情大全網