2012-11-14 77 views
-2

我沒有得到這個程序正確顯示我分期付款,我可以請得到一些幫助的感謝......確定定期貸款支付

package Loops; 

import java.util.Scanner; 

/** 
* 
* 
*/ 
public class program { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     //variabled decleared 
     double rate; 
     double payment; 
     //input 
     System.out.print("Enter Loan Amount:"); 
     double principal = input.nextDouble(); 
     System.out.print("Enter Annual Interest:"); 
     double interest = input.nextDouble(); 
     System.out.print("Total payments per year:");//12=monthly,4= quartely,2=semi-annually and 1=annually 
     double period = input.nextDouble(); 
     System.out.print("Enter Loan Length :"); 
     int length = input.nextInt(); 

     //proces 
     double n = period * length; 
     rate = interest/100; 
     double monthly_rate = rate/period; 
     payment = principal * (principal * (monthly_rate * Math.pow((1 + monthly_rate), n))); 

     System.out.printf("Your Monthly sum is %.2f", payment); 


    } 
} 
+4

你期待什麼,你有什麼? – kosa

+2

不是答案,只是遵循java命名約定。包名應以小寫字母開頭,類名應以大寫字母開頭。 – PermGenError

+0

我越來越375,但它假設爲750,因爲我輸入500美元和1年50%的利息...... –

回答

0

嘗試此公式:

//this is how much a monthly payment is 
payment = (rate + (rate/((Math.pow(1 + rate), n) -1)) * principal 

這是基於斷第一谷歌的結果爲式中的一個。如果錯誤,請發佈結果和期望答案。

我很確定你的公式剛剛關閉,正如你在上面說的那樣,應該有一個分母在方程中。

You can use r* Math.pow ((1+r),n) to calculate the numerator and part of the denominator

+0

非常感謝,它效果很好 –

1
principal = 50000; //Redacted. Eating my words. 

period = 4; 
length = 4; 
n = 16; 

rate = 0.085; 
monthly_rate = 0.085/16 = 0.0053125; 

payment = 50000 * 50000 * 0.0053125 * (1 + 0.0053125)^16; 
     = 2.5x10^9 * 0.0053125 * 1.088; 
     = Something remainingly massive 

基本上...您的公式是錯誤的。你不需要除以的功商嗎?你的來源在哪裏?

payment = principal * (rate + (rate/(Math.pow(1 + rate, n) - 1))); 

Source

實施例:

payment = 50000*(0.085+(0.085/(1.085^16-1))) 
= 5830.68 
+0

payment = principal * principal *(monthly_rate *(1 + monthly_rate)^ n);我得到一個錯誤,說二進制運算符的操作數類型錯誤 –

+0

請注意,java中的'^'是按位異或的。你應該使用Math.pow()。 –

+0

實際上他的拼寫是好的。 http://en.wikipedia.org/wiki/Principal_sum#principal_sum –