我错过了什么循环来生成我的贷款配置?

时间:2016-05-06 05:32:37

标签: java loops math methods

我的java程序假设接收硬编码的贷款信息并生成一个输出,显示付款,余额,付款金额,支付利息,本金申请和新余额,持续1-60天。我在下面的代码中将评论放在我需要帮助的领域。另外,我的输出现在还没有整齐地格式化。任何帮助都将非常感激。

public static void main(String[] args) {
    // TODO Auto-generated method stub

    double loanAmount = 25000.00; 
    double annualInt = 0.0525;
    double numberOfMonths = 60.00;
    double monthlyPayment;
    double rate;
    double balance;
    double intPaid=0;
    double prAppl=0;
    int paymentsMade;


    System.out.println("Principal :   "+loanAmount);
    System.out.println("Annual Int:   "+annualInt*100+"%");
    System.out.println("Term (mo) :   "+numberOfMonths);


    paymentsMade      = numOfPaymentsMade(numberOfMonths);
    rate             = calcMonthlyInterestRate(annualInt);
    monthlyPayment   = calcMonthlyPayment(rate,loanAmount,numberOfMonths);
    balance          = calcBalance(loanAmount,rate,numberOfMonths,monthlyPayment);
    intPaid          = interestPaid(rate,balance,intPaid);
    prAppl           = prApp(monthlyPayment,intPaid,prAppl);
    displayResults(numberOfMonths,rate,loanAmount,paymentsMade,balance,intPaid,monthlyPayment,prAppl);

}
   public static int numOfPaymentsMade(double numberOfMonths)
{
       int paymentsMade=0;
       for(paymentsMade=1; paymentsMade<numberOfMonths+1; paymentsMade++)
     {
        System.out.println(paymentsMade);
     }

    return paymentsMade;
}
//--------------------------------------------------------------------------------------------------------
    public static double calcMonthlyInterestRate(double annualInterestRate){
        double monthlyInterestRate;
        monthlyInterestRate = (annualInterestRate/12);
        return monthlyInterestRate;
}
//--------------------------------------------------------------------------------------------------------
    public static double calcMonthlyPayment(double rate, double loanAmount, double  numberOfMonths )
    {
       double monthlyPayment;
       int i;

       monthlyPayment = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));

        for(i=0; i<60; i++)
        {
         System.out.println(monthlyPayment);
        }
        return monthlyPayment;
}
//--------------------------------------------------------------------------------------------------------
    public static double calcBalance(double loanAmount, double rate, double numberOfMonths, double monthlyPayment)
    {
        double balance=0;
        // this method needs fixing

        for(balance=loanAmount; balance<monthlyPayment-1; balance--){
            balance = (loanAmount-monthlyPayment)+(balance*rate);
        }
        return balance;
    }
//--------------------------------------------------------------------------------------------------------
    public static double interestPaid(double rate, double balance, double intPaid)
    {   
        // need a loop corresponding with the balance
        intPaid=balance*rate;

        return intPaid;
    }
//--------------------------------------------------------------------------------------------------------
    public static double prApp(double monthlyPayment, double intPaid, double prAppl)
    { 
        // need a loop corresponding with intPaid
        prAppl=monthlyPayment-intPaid;

        return prAppl;
    }
 //--------------------------------------------------------------------------------------------------------
    public static void displayResults (double numberOfMonths, double rate, double loanAmount, double paymentsMade, double balance, double intPaid,  double monthlyPayment, double prAppl){
        double monthPay;
        monthPay = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));

        System.out.println("Payment  :"+monthPay);
        System.out.println("");
        System.out.println("Pmt  Balance    Payment   Int Pd  Prin Appl  New Bal");
        System.out.println("--- ---------- --------- -------- --------- ----------");
        System.out.println(paymentsMade+" "+balance+" "+monthlyPayment+" "+intPaid+" "+prAppl); 

    //maybe a method should be added for the new balance section
    //how do I add all of the computations up?

}     }

1 个答案:

答案 0 :(得分:1)

此循环有三个问题:

    for(balance=loanAmount; balance<monthlyPayment-1; balance--){
        balance = (loanAmount-monthlyPayment)+(balance*rate);
    }

首先,balance--从余额中减去1。没有理由你想这样做(好吧,如果抵押权人是银行家的亲戚,无偿地减少他们每个月的余额将是一个很好的小礼物,但否则你不会想这样做)。绝大多数for循环涉及索引,每次循环增加1或减1。但情况并非如此,所以你不想要balance--。循环的主体应该减少平衡,所以你不需要任何东西来循环的第三部分。你可以简单地写:

for (balance = loanAmount; balance < monthlyPayment - 1; ) {

但我倾向于使用while循环。以上相当于:

balance = loanAmount;
while (balance < monthlyPayment - 1) { ... }

这意味着只要余额低于每月付款,循环就会继续。

这是第二个问题。只要余额高于每月付款,您希望循环继续运行,并在它低于下限时停止。这意味着for循环中的条件是向后的(与我的while循环示例中的条件相同)。你可能想要

    for (balance = loanAmount; balance >= monthlyPayment - 1; ) {

第三个问题是身体中的公式是错误的。您想确定支付的本金金额,并减少该金额的余额。你的循环体几乎可以做到这一点,但这是一个很大的错误。一旦你解决了循环标题的问题,我就会让你想出这个。