# use while loop to output the sum of all even numbers

i need to prompt the user to input 2 integers: firstNum & secondNum (firstNum must be less than secondNum).
but my main problem is that i got the unexpected output when i try to output the sum of all even numbers between these 2 integers.
Here's my codes:

import java.io.*;
import java.util.*;

public class EvenNumber
{

public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n, sum, rem;

n = 0;
sum = 0;

System.out.flush();

System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();

rem = firstNum%2;

while(firstNum < (secondNum - 2))
{
if(rem == 0)
{
{
firstNum = firstNum + 2;
n++;
{
sum +=firstNum;
}
}

System.out.print(sum);
}

else
{
{
firstNum = firstNum + (2*n + 1);
n++;
{
sum += firstNum;
}
}

System.out.print(sum);
}
}
}
}

i input 3 and 17, then the output should be 47,
but it outputs "4112372".
thanks
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

int accumulator = 0;
while (++firstNum < secondNum) {
if (firstNum % 2 != 0)
System.out.println(firstNum);
else {
accumulator += firstNum;
}
}
System.out.println(accumulator);
Commented:
Better with two pairs of curly braces:

int accumulator = 0;
while (++firstNum < secondNum) {
if (firstNum % 2 != 0) {
System.out.println(firstNum);
}
else {
accumulator += firstNum;
}
}
System.out.println(accumulator);
Author Commented:
I input 3 and 17, then the output should be 47
but it shows "70".............
Commented:
These are the even numbers between 3 and 17. Their sum is seventy:

3
------------ 4
5
------------ 6
7
------------ 8
9
------------ 10
11
------------ 12
13
------------ 14
15
------------ 16
17
------------  +
------------ 70

Experts Exchange Solution brought to you by