import java.math.BigDecimal;
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),古交企業(yè)網(wǎng)站建設(shè),古交品牌網(wǎng)站建設(shè),網(wǎng)站定制,古交網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,古交網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
/**
* 銀行還款計(jì)算
* @author cuiran
* @version TODO
*/
public class BankRefund {
/**
*
* 等額本金還款法【利息少,但前期還的多】
* @param totalMoeny 貸款總額
* @param rate 貸款商業(yè)利率
* @param year 貸款年限
*/
public static void principal(int totalMoney,double rate,int year){
/**
* 每月本金
*/
int totalMonth=year*12;
double monthPri=totalMoney/totalMonth;
/**
* 獲取月利率
*/
double monRate=resMonthRate(rate);
BigDecimal b = new BigDecimal(monRate);
monRate = b.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
for(int i=1;i=totalMonth;i++){
double monthRes=monthPri+(totalMoney-monthPri*(i-1))*monRate;
BigDecimal b1 = new BigDecimal(monthRes);
monthRes = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("第"+i+"月,還款為:"+monthRes);
}
}
/**
*
* 等額本息還款【利息多】
* @param totalMoeny 貸款總額
* @param rate 貸款商業(yè)利率
* @param year 貸款年限
*/
public static void interest(int totalMoney,double rate,int year){
/**
* 獲取月利率
*/
double monRate=resMonthRate(rate);
/**
* 月還款本息
*/
double monInterest=totalMoney*monRate*Math.pow((1+monRate),year*12)/(Math.pow((1+monRate),year*12)-1);
BigDecimal b = new BigDecimal(monInterest);
monInterest = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("月供本息和:"+monInterest);
}
/**
*
* 轉(zhuǎn)換為月利率
* @param rate
* @return
*/
public static double resMonthRate(double rate){
return rate/12;
}
/**
* TODO
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int totalMoney=430000;
double rate=0.0655;
int year=20;
// BankRefund.interest(totalMoney, rate, year);
BankRefund.principal(totalMoney, rate, year);
}
}
截圖:題目翻譯過來的大概意思和程序代碼:譯文:編寫一個(gè)程序,使之能顯示同每月按揭貸款還款額以及欠款余額,然后顯示還款中有多少是利息還款,有多少是本金還款(即有多少還款是真正用來減少債務(wù)的)。假設(shè)年利率是7.49%。命名一個(gè)常量來代表利率。注意還款按月進(jìn)行,所以利率只是年利率7.49的1/12。 代碼:注:按揭貸款有兩種月供還款方式:本金還款和本息還款,題目要求的是按“本息還款”方式進(jìn)行編程,再程序中我把兩種還款方式都寫了出來,關(guān)鍵地方有注釋!
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; //付款次數(shù)
int month=1;
public static void main(String[] args){
Repay rp=new Repay();
rp.payback();
}
public void payback(){
System.out.println("請(qǐng)輸入借款金額");
//獲得貸款數(shù)額
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("請(qǐng)選擇還款方式:輸入1選擇等額本金還款,輸入2選擇等額本息還款");
int mode=sc.nextInt();
//等額本金還款
if(mode==1){
System.out.println("您總共借款"+debt_fn+";還款方式:等額本金還款;還款時(shí)間:1年"+";年利率是:"+nll+";月利率"+mll);
System.out.println("分期還款明細(xì)");
double monthPincipal=debt/12; //每月應(yīng)還本金
debt=monthPincipal*12;
double accrualM; //每月還款利息
double tm; //每月還款金額
//分期還款明細(xì)
while(debt=1){
accrualM=debt*MLL;
tm=monthPincipal+accrualM;
debt=debt-monthPincipal;
if(debt1){
debt=0;
}
//把小數(shù)位數(shù)格式化成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+" 本月應(yīng)還本金:"+monthPincipal_fn+" 本月還款利息:"+accrualM_fn+" 剩余本金:"+debt_fn2);
month++;
}
}
//等額本息還款
if(mode==2){
System.out.println("您總共借款"+debt_fn+";還款方式:等額本息還款;還款時(shí)間:1年"+";年利率是:"+nll+";月利率"+mll);
//等額本息還款的月還款數(shù)公式
double X=debt*MLL*(Math.pow((1+MLL), MONTH))/(Math.pow((1+MLL), MONTH)-1);
String X_fn=fn.format(X); //格式化小數(shù)位數(shù)
System.out.println("您的月還款額為:"+X_fn);
//分期還款明細(xì)
double lixiM,benjinM; //月利息,月本金
System.out.println("分期還款明細(xì)");
while(debt=1){
lixiM=debt*MLL;
benjinM=X-lixiM;
debt=debt-benjinM;
if(debt1){
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+" 本月應(yīng)還本金(即減少債務(wù)的錢):"+benjinM_fn+" 本月還款利息:"+lixiM_fn+" 剩余本金:"+debt_fn3);
month++;
}
}
}
}
等額本息和等額本金是常用的貸款償還方式,下面是它們的計(jì)算機(jī)編程代碼示例:
## 等額本息
def calcEqualInstallment(principal, annual_rate, months):
r = annual_rate / 12
numerator = principal * r * (1 + r) ** months
denominator = (1 + r) ** months - 1
monthly_payment = numerator / denominator
return monthly_payment
# 示例
principal = 100000 # 本金
annual_rate = 0.06 # 年利率
months = 24 # 期數(shù)
monthly_payment = calcEqualInstallment(principal, annual_rate, months)
print("每月還款金額為:", round(monthly_payment, 2))
## 等額本金
def calcEqualPrincipal(principal, annual_rate, months):
r = annual_rate / 12
monthly_principal = principal / months # 每月本金
monthly_payments = [] # 每月還款額列表
balance = principal
for i in range(months):
interest = balance * r # 每月利息
monthly_payments.append(monthly_principal + interest)
balance -= monthly_principal
return monthly_payments
# 示例
principal = 100000 # 本金
annual_rate = 0.06 # 年利率
months = 24 # 期數(shù)
monthly_payments = calcEqualPrincipal(principal, annual_rate, months)
for i, monthly_payment in enumerate(monthly_payments):
print("第{}個(gè)月的還款額為:{}".format(i+1, round(monthly_payment, 2)))
請(qǐng)注意,本示例中的計(jì)算方式僅作為一般示例,實(shí)際計(jì)算還需要考慮更細(xì)致的情況,例如還款日、手續(xù)費(fèi)、逾期利率等。
import javax.swing.JOptionPane;
public class Pay {
public static void main(String args[]){
String loanString = JOptionPane.showInputDialog("請(qǐng)輸入貸款本金( loanAmout):such as 20000.00") ;
double loanAmount= Double.parseDouble(loanString);
String dateString = JOptionPane.showInputDialog("請(qǐng)輸入貸款期(loanDate):between 24-60");
int loanDate = Integer.parseInt(dateString);
String monthRateString = JOptionPane.showInputDialog("請(qǐng)輸入月利率 (MonthRate):such as 0.00005");
double monthRate = Double.parseDouble(monthRateString);
double pay_Per_Month = (loanAmount+loanAmount * loanDate * monthRate)/loanDate;
JOptionPane.showMessageDialog(null, pay_Per_Month);
}
}