求写一个Java小程序~~

Write a program that reads the amount of a monthly mortgage payment and
the amount still owed—the outstanding balance—and then displays the
amount of the payment that goes to interest and the amount that goes to
principal (i.e., the amount that goes to reducing the debt). Assume that the
annual interest rate is 7.49 percent. Use a named constant for the interest rate.
Note that payments are made monthly, so the interest is only one twelfth of
the annual interest of 7.49 percent.

截图:题目翻译过来的大概意思和程序代码:译文:编写一个程序,使之能显示同每月按揭贷款还款额以及欠款余额,然后显示还款中有多少是利息还款,有多少是本金还款(即有多少还款是真正用来减少债务的)。假设年利率是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++;
}
}

}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-11
我去你这是考试题啊!!还是笔试题。。能不能翻译成汉语
第2个回答  2013-11-11
FENGXIANGSHENG 你太强了。。
相似回答