jamcoupe
jamcoupe

Reputation: 1452

Java simple while loop confusion

I am having trouble with this simple loop for a uni lab. It wont stop looping until I put in a number over a thousand. I can't see where I have gone wrong on such a simple loop.

I have meant to have written a simple method that will loop through adding numbers until the number is greater than 100 and then once it has reached 100 or greater it will output the total.

public void adder2(){
    int sum = 0;
    int number = 0;
    while(number < 100){
        sum = sum + number;
        number = getNumber();
    }
    System.out.println("The result is " + sum);
}

Upvotes: 0

Views: 353

Answers (1)

kol
kol

Reputation: 28728

Try this: while (sum < 100 && number < 100) { ...code as above... }

Upvotes: 1

Related Questions