Java Program - Summing Integers
Hi guys--I have to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers.
For instance, if the numbers were: 9, 10, 17, -20, 22, -3
evens should be 12
odds 23
all 35
I'm messing up the math but here's what I have below--help!!!
import java.util.*;
public class Integers {
static Scanner console = new Scanner(System.in);
static final int SENTINEL = -999;
public static void main(String[] args) {
int number;
int sumAll = 0;
int sumEven = 0;
int sumOdd = 0;
System.out.println("Enter integers, positive, negative, or zeros ending with " + SENTINEL);
number = console.nextInt();
System.out.println();
int count = 0;
while (number != SENTINEL) {
sumAll += count;
number = console.nextInt();
if (count % 2 == 0)
sumEven += count;
else
sumOdd += count;
count++;
}
System.out.println("Sum of all integers: " + sumAll);
System.out.println("Sum of the even integers: " + sumEven);
System.out.println("Sum of the odd integers: " + sumOdd);
}
}