for these guidelines:
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, separated by a space and on a single line, the sum of all the even integers read and the sum of all the odd integers read. Declare any variables that are needed.
ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.
I wrote this code, but it the HW software will not except it due to a logical error. I cant seem to find the logical error here. can someone point out what is wrong with it?
int sumP=0;
int sumO=0;
Scanner stdin = new Scanner (System.in);
System.out.println("enter an odd or even number");
while (stdin.nextInt() >= 0){
if(stdin.nextInt()%2 == 0)
sumP+= stdin.nextInt();
else
sumO += stdin.nextInt();
}
System.out.println(sumP + " " + sumO);
You need to save the value you have read, otherwise you will be used different values in the while loop and the addition.