2017-11-18 91 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); 

    } 



}