2015-05-09 138 views
1

我希望這不是一個明顯的問題,但我是Java新手,我一直在創建一個複利計算器。我不想採用用戶輸入和計算它們的所有值。關於用Java複數算術運算的困惑

P = present value, 
r = rate, 
m = times compounded in a year, 
t = years compounded. 
A = the amount at the end of the term 

A是我正在尋找。化合物利益的公式爲

A = P(1+r/m)^mt

A = Math.pow((P*(1+r/m)),m*t); 
    System.out.println("The amount(A) equals "+A); 

我覺得我可以知道爲什麼計算是不工作的權利,但我不知道正確的方式。

+1

嗨,你能告訴我什麼是輸入和預期的輸出是什麼? – Suspended

+0

輸入是雙倍的,因爲可能有小數。 P = 555 r = 0.05 m = 1 t = 2並且A是需要找到的那個。 A應該是$ 687.50 – Garrett777

回答

0

這應該解決您的問題

double amount,Principal ; 
int r,m,t; 
Amount = Principal * Math.pow((1+(r/m)) , m*t); 
System.out.println("The amount(A) equals "+amount); 
+0

謝謝,它工作! – Garrett777

+0

換句話說,OP需要在'Math.pow'調用之外移動乘以'P'的乘積。 – Teepeemm

0

我相信你公式不正確。你需要這樣寫:

public static void main(String[] args) { 
     int p = 100; 
     int t = 5; 
     int r = 10; 
     int m = 2; 
     double amount = p * Math.pow(1 + (r/m), m * t); 
     double interest = amount - p; 
     System.out.println("Compond Interest is " + interest); 
     System.out.println("Amount is " + amount); 

    }