Loops do not hold values

0

Hello! I'm trying to develop a simple program that does an automatic accounting calculation, however, the condition within the for (while, do, while, if / else, any) does not keep its values. The repetition is executed 12 ~ 13 times and in all the output value is not changed. Why exactly the values inside the loops are zeroed and displayed as if only one account was made?

public static void main(String[] args) 
    {
      int x;
      double percentual=0.005, ValorSaida, ValorPercentual=0;
      x=Integer.parseInt(JOptionPane.showInputDialog("Digite o valor depositado mensalmente"));

      for(int meses=0; meses<=12; meses++)
      {  

       ValorPercentual=x*percentual;
       ValorSaida=x+ValorPercentual;
       JOptionPane.showMessageDialog(null, ""+ValorSaida+" "+ValorPercentual);

      }     


    }
    
asked by anonymous 23.10.2014 / 15:33

1 answer

2

Every time the code executes the loop, it under-writes the values try like this:

public static void main(String[] args) 
    {
      int x;
      double percentual=0.005, ValorSaida, ValorPercentual=0;
      x=Integer.parseInt(JOptionPane.showInputDialog("Digite o valor depositado mensalmente"));

      for(int meses=0; meses<=12; meses++){  

          ValorPercentual += x*percentual;
          ValorSaida += x+ValorPercentual;

      }     
      JOptionPane.showMessageDialog(null, ""+ValorSaida+" "+ValorPercentual);

    }

You have to add the values - > with +=

    
23.10.2014 / 15:38