Add results and display separately within the loop

Asked

Viewed 427 times

1

I would like to know how to add up several results. I’m creating a simple application for a particular financial challenge. For example:

double salario = 1500;
double desafio;

for(double mes=1; mes<=12; mes++){
    desafio = (salario * mes)/100;
    System.out.println("Valor do Desafio:" + desafio);
}

This code generates as a result all the monthly values that need to be "deposited", perfect. I wanted to know how to add all these results generated.

  • Perfect.. I can use... but after generating all the results, how do I add up everything that generated?

2 answers

3


Would that be?

double salario = 1500;
double desafio = 0;

for(double mes=1; mes<=12; mes++){
    desafio += (salario * mes)/100;
    System.out.println("Valor do Desafio:" + desafio);
}

This will add up the previous values to the current ones. See working on ideone: https://ideone.com/cxw87K

If you want to continue displaying the values of each month, just create a temporary variable within the loop:

double salario = 1500;
double desafio = 0;

for(double mes=1; mes<=12; mes++){
    double mesAtual = (salario * mes)/100;
    desafio += mesAtual;
    System.out.println("Valor do Mes Atual:" + mesAtual);
}

System.out.println("Valor do desafio:" + desafio);

The exit will be:

Valor do Mes Atual:15.0
Valor do Mes Atual:30.0
Valor do Mes Atual:45.0
Valor do Mes Atual:60.0
Valor do Mes Atual:75.0
Valor do Mes Atual:90.0
Valor do Mes Atual:105.0
Valor do Mes Atual:120.0
Valor do Mes Atual:135.0
Valor do Mes Atual:150.0
Valor do Mes Atual:165.0
Valor do Mes Atual:180.0
Valor do desafio:1170.0

See it working on ideone: https://ideone.com/N1IKrg

  • This '+=' generated error for me... What I need is to take the results that generated in the calculation.. and add everything... I think it’s pretty simple, but I’m still confused

  • @Mauritius does not cause an error, see: https://ideone.com/cxw87K

  • No friend. this of generating the values it is already perfect... but when I added the '+=' in the eclipse it generated this: Exception in thread "main" java.lang.Error: Unresolved Compilation problem: The local variable challenge may not have been initialized at financial.main(financial.java:9)

  • @Mauríciomaciel Did you start the variable challenge? Pay close attention to my code, notice that I started it: double desafio = 0; Access the link and see that the code works perfectly.

  • Yes, I saw that you started, I did the same now and I was sorry for the failure... but look at... Run this code here (my initial code): public class financial { public Static void main(String[] args) { double salario = 1500; double challenge; double result; for(double mes=1; mes<=12; mes++){ challenge = (salary * month)/100; System.out.println("Challenge Value:" + challenge); } } } See the results they generate.. now see the results they generate with your suggested change

  • @Mauríciomaciel and what’s the problem? You said you want to sum all the values and in your code do not sum, it only displays the value of the current loop iteration.

  • Yes, I want to add... But I want to do so... generated there the values "15, 30, 45, 60..." after generating the 12 months with their respective values, I want to add how much gave.... type 15+30+45... I could even create a 'challenge' variable for each month and add it all up later, but I think there’s another one more correct and simple to do.. If I haven’t made myself clear, please ask me.

  • This... type after generating all the results.. wanted a new variable to show the total sum of everything it generated...

  • @You didn’t say that in the question, so it’s important to ask the question and explain exactly what you want to do and not just a piece of the problem. This is easily solved by creating a new variable. See the edit in the reply.

  • Sorry... I thought I explained it well, I’m new around here, as I’m sure you’ve noticed... But Tchê 100%, this is exactly what I needed, very grateful and again sorry for the inconvenience. Thanks Dr..

  • @Mauríciomaciel worked cool now?

  • Perfect.. That’s what I was trying to complete

Show 7 more comments

0

Declare and initialize a variable to receive the total:

double total = 0;

to add the values use the following command (inside the loop):

total += desafio;

practically amounts to total = total + desafio;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.