2016-12-02 24 views
0
public static double capitalAfterYearsRecursive(double K0, int n, double i,double R) { 
    double K1; 
    if(n < 0) 
    { 
     return K0; 
    } 
    else 
    return capitalAfterYearsRecursive(K0, n-1 ,i ,R)*(i+1); 
} 

所以,球員,我需要你的幫助,它不顯示了正確的平衡:/計算資金賬戶的銀行在遞歸

K0 start capital 
n runtime (years) 
i interest rate 
R yearly rate 
+1

歡迎StackOverflow上。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在您發佈代碼並準確描述問題之前,我們無法有效幫助您。 – Prune

+0

您可以添加您想要計算的參數數據的示例,以及預期的結果嗎? – martiendt

+0

你爲什麼通過「利率」和「年率」?他們獨立的是什麼?你從不在計算中使用** R **。 – Prune

回答

0
public static double capitalAfterYearsRecursive(double K0, int n, double i,double R) { 

    //double K1; 
    if(n <= 0) 
    { 
     return K0; 
    } 
    else 
    return capitalAfterYearsRecursive(K0, n-1 ,i ,R)*(i+1)+R; 
} 

好了,問題是,我不得不添加+ R

+0

所以選擇自己anwser爲正確anwser。 – glee8e

1

我相信,你最初的問題是基礎條件。您運行遞歸回年-1,而不是0年如何

if (n <= 0) 

...

試試你的代碼爲0的時間段:你的結果應該是原始本金。