2017-11-18 145 views
-6

首先,我是一個相對較新的程序員,因爲它屬於JAVA。我搜索了很多東西,但有可能我沒有找到正確的東西。我基本上試圖從第一個「年」到第十個「年」列出一個起始金額,乘以百分比,並加到起始金額上。然後,我想採取這個新的數額,並一遍又一遍地做同樣的事情,直到它達到10或者甚至是指定的總數:2000。JAVA循環公式

public static void main(String[] args) 
{ 
    double startAmount = 1000; 
    double ratePercentage = 0.05; 
    double newAmount; 


    for (int i = 0; i <= 10; i++){ 
     newAmount = startAmount + (startAmount * ratePercentage); 
     System.out.println(i + " " + newAmount); 

    } 



} 
+0

歡迎來到Stackoverflow!請花點時間回顧一下以下鏈接,以便了解如何提出能夠獲得最佳答案的最佳機會的問題。 https://stackoverflow.com/help/how-to-ask – tarheel

回答

1

試試這個

public static void main(String[] args) 
{ 
double startAmount = 1000; 
double ratePercentage = 0.05; 
double newAmount; 


for (int i = 0; i < 10; i++){ 
    newAmount = startAmount + (startAmount * ratePercentage); 
    System.out.println(i + " " + newAmount); 
    startAmount=newAmount; 

} 

} 
2

我不是100%肯定你正在嘗試才達到的。但假設你正在嘗試做某種複合興趣。這應該有所幫助。

public static void main(String[] args) 
{ 
    double startAmount = 1000; 
    double ratePercentage = 0.05; 
    double newAmount; 

    newAmount = startAmount; 

    for (int i = 0; i <= 10; i++){ 
     newAmount = newAmount + (newAmount * ratePercentage); 
     System.out.println(i + " " + newAmount); 

    } 



}