0

I'm bit lost here. I have a for loop which increases a value, something like this:

int nu01 = 10000;
int level = 0;
int increase = 35000;

for (int i = 0; i < 100; i++) {
    nu01 = nu01 + increase;
    level++;
    System.out.println(nu01);

    if (level > 50) {
        increase = 45000;
    }

This works fine, but I need sum of all numbers from loop as total:

loop: 10,20,30,40,50,70,90,120....
total:10,30,60,100,150,220,310,430...

I tried:

int total;
total=nu01 + nu01; //or  nu01 + nu01 + increase;

But I get strange sums. So I need loop which increase numbers and sum of all those numbers. I hope this makes sense. Thanks.

3 Answers 3

2

what you want to do is something along the lines of

int total = 0;
...
//beginning of your for loop
total = total + nu01; // alternatively you could do total += nu01;
Sign up to request clarification or add additional context in comments.

Comments

0

I might be misunderstanding you here but I believe you want something like this

public class forLoops{

    public static void main(String args[]){

        //Initialisation
        int level = 0;
        int increase = 35000;
        int total = 10000;

        //For Loop
        for(int i = 0; i < 100; i++){

            total += increase; //Same as total = total + increase;
            level++;
            System.out.println(total);

        if(level >= 50){

            increase = 45000;

        }
        }
    }
 }

Comments

0

declare a total variable then store the tota int total =0;

total += nu01;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.