2015-10-07 124 views
0

我正在嘗試編碼貸款計算器。我在計算每月付款時遇到問題。我試圖將不同利率的每月付款從低到高打印出來,增加0.25%。Java loanTable計算器

我懷疑我的while循環有什麼問題。有什麼建議麼?

我總是感謝你的幫助。我正在努力改善我的問題。

import apcslib.Format; 
import chn.util.*; 

public class loanTable { 


    public static void main(String[] args) { 
     //Declaring Variables 
     double k; 
     double n; 
     double c; 
     double p; 
     double a; 
     double low; 
     double high; 

     int time; 

     //Getting User Input 
     ConsoleIO console = new ConsoleIO(); 

     System.out.println("Amount of the loan: "); 
     p = console.readDouble(); 
     System.out.println("Principal = $" + p); 

     System.out.println("The length of loan in years: "); 
     time = console.readInt(); 
     System.out.println("Time = " + time + " years"); 

     System.out.println("A low interest rate in %: "); 
     low = console.readDouble()/100; 
     System.out.println("Low rate = " + low + "%"); 

     System.out.println("A high interest rate in %: "); 
     high = console.readDouble()/100; 
     System.out.println("High rate = " + high + "%"); 

     //While Loop 
     while (low <= high) { 

      k = low/1200.0; 
      n = time * 12; 
      c = Math.pow((1 + k), n); 
      a = (p * k * c)/(c - 1); 

      System.out.println("Annual interest rate: " + low * 100); 
      System.out.println("Monthly Payment: " + Format.left(a, 2, 2)); 

      low += (0.25/100); 
     } 
    } 

} 

`

+2

你說,你有輸出的麻煩,但沒有告訴我們**什麼**麻煩。請通過具體和充分的描述來幫助我們。 –

+0

我的輸出,年利率11.0 每月還款金額:282.40 年利率:11.25 每月還款金額:282.50 年利率:11.5 每月還款金額:282.61 年利率:11.75 每月還款金額:282.72 –

+0

他們應該是, 11.00 952.32 11.25 971.26 11.50 990.29 11.75 1009.41 12.00 1028.61 –

回答

1

的錯誤是在k的計算。 應該

k = low /12; 

下面是正確的版本

public class loanTable { 

public static void main(String[] args) { 
    //Declaring Variables 
    double k; 
    double n; 
    double c; 
    double p; 
    double a; 
    double low; 
    double high; 

    int time; 

    System.out.println("Amount of the loan: "); 
    p = 100000; 
    System.out.println("Principal = $" + p); 

    System.out.println("The length of loan in years: "); 
    time =30 ; 
    System.out.println("Time = " + time + " years"); 

    System.out.println("A low interest rate in %: "); 
    low = 0.11; 
    System.out.println("Low rate = " + low + "%"); 

    System.out.println("A high interest rate in %: "); 
    high = 0.1125; 
    System.out.println("High rate = " + high + "%"); 

    //While Loop 
    while (low <= high) { 
     //changed calculation of k 
     k = low /12; 
     n = time * 12; 
     c = Math.pow((1 + k), n); 
     a = (p * k * c)/(c - 1); 

     System.out.println("Annual interest rate: " + (low * 100)); 
     System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/); 

     low += (0.25/100); 
    } 
    } 
} 

要回答第二個問題(」我得到的輸出,直到年利率至11.75,但我需要以遞增到12.00會如何我這樣做?「):由於四捨五入錯誤,while循環停在11.75。看到意見和一些替代方案來克服它:

public class LoanTable { 

    public static void main(String[] args) { 
     //Declaring Variables 
     double k; 
     double n; 
     double c; 
     double p; 
     double a; 
     double low; 
     double high; 

     int time; 

     System.out.println("Amount of the loan: "); 
     p = 100000; 
     System.out.println("Principal = $" + p); 

     System.out.println("The length of loan in years: "); 
     time =30 ; 
     System.out.println("Time = " + time + " years"); 

     System.out.println("A low interest rate in %: "); 
     low = 0.11; 
     System.out.println("Low rate = " + low + "%"); 

     System.out.println("A high interest rate in %: "); 
     high = 0.12; 
     System.out.println("High rate = " + high + "%"); 

     System.out.println("---------------- Not working well ------------------"); 
     //While Loop 
     while (low <= high) { 
      /* 
      * changed calculation of k 
      */ 
      k = low /12; 
      n = time * 12; 
      c = Math.pow((1 + k), n); 
      a = (p * k * c)/(c - 1); 

      System.out.println("Annual interest rate: " + (low * 100)); 
      System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/); 

      low += (0.25/100); 

      /* 
      * at the 4th loop low = 0.12000000000000001 
      * so low > high and while block is terminated. 
      */ 
      System.out.println("low = "+ low); 

     } 

     System.out.println("---------------- Alternative 1 ------------------"); 
     //While Loop 
     low = 0.11; 
     high = 0.12; 
     int counter = 0; 
     double interest = low; 

     while ( interest <= high ) { 

      /* 
      * changed calculation of k 
      */ 
      k = interest /12; 
      n = time * 12; 
      c = Math.pow((1 + k), n); 
      a = (p * k * c)/(c - 1); 

      System.out.println("Annual interest rate: " + (interest * 100)); 
      System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/); 

      counter++; 
      /* 
      * recalculate interest from low, to avoid accumulating 
      * rounding error 
      */ 
      interest = low + ((0.25/100)* counter); 
     } 

     System.out.println("---------------- Alternative 2 ------------------"); 
     //While Loop 
     low = 0.11; 
     high = 0.12; 

     /* 
     * cast float to reduce the accuracy. A better alternative would be 
     * to declare low and high as float 
     */ 
     while ( (float)low <= (float)high ) { 
      /* 
      * changed calculation of k 
      */ 
      k = low /12; 
      n = time * 12; 
      c = Math.pow((1 + k), n); 
      a = (p * k * c)/(c - 1); 

      System.out.println("Annual interest rate: " + (low * 100)); 
      System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/); 

      low += (0.25/100); 

     } 

     System.out.println("---------------- Alternative 3 ------------------"); 

     //While Loop 
     /* 
     * work with integer 
     */ 
     int lowInterest = 1100; 
     int highInterest = 1200; 

     while ( lowInterest <= highInterest ) { 
      /* 
      * changed calculation of k 
      */ 
      k = (float)lowInterest /(12*10000); 
      n = time * 12; 
      c = Math.pow((1 + k), n); 
      a = (p * k * c)/(c - 1); 

      System.out.println("Annual interest rate: " + ((float)lowInterest /100)); 
      System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/); 

      lowInterest += 25; 

     } 

     } 
    } 
+0

哇謝謝,工作。最後一個問題我得到的輸出,直到年利率爲11.75,但我需要增加到12.00我該怎麼做? –

+0

@MichaelGuest:現在是一個很好的StackOverflow公民:如果這個答案對你有幫助,你應該記得投票,如果它回答你的問題,然後[接受](http://stackoverflow.com/help/someone-答案)它。 –

1

讓我們假設你正在使用:

  • 我:年息11%
  • ,P:週期:貸款$ 100,000個
  • N中的現值30年

calc的正常計算公式ulating每月還款是:

(P*I/12)/(1-(1+I/12)^(-n*12)) 

或者,在Java:

private double monthlyRepayment(double loanValue, double annualInterest, int termInYears) { 
    double monthlyInterest = annualInterest/12; 
    double termInMonths = termInYears * 12; 
    return loanValue * monthlyInterest/(1 - Math.pow(1 + monthlyInterest, -termInMonths)); 
} 

這正確地計算還款爲$ -952.32

+0

感謝這個公式幫了我很多! –

+0

@MichaelGuest:現在是一個很好的StackOverflow公民:如果這個答案對你有幫助,你應該記得投票,如果它回答你的問題,然後[接受](http://stackoverflow.com/help/someone-答案)它。 –