The complete program is given below.
You could have answered the question by mechanically writing in a statement to increment count, as the documentation suggests. Hopefully you understand the larger context of what is going on. Often in programming, several variables have to be kept coordinated like this. It is something of a juggling act.
int count = 0;
// get the first value
System.out.println( "Enter first integer (enter 0 to quit):" );
inputData = userin.readLine();
value = Integer.parseInt( inputData );
while ( value != 0 )
{
//add value to sum
sum = sum + value;
//increment the count
count = count + 1;
//get the next value from the user
System.out.println( "Enter the " +
(count+1) + "th integer (enter 0 to quit):" );
inputData = userin.readLine();
value = Integer.parseInt( inputData );
}
System.out.println( "Sum of the " + count + " integers: " + sum );
}
}
Let us work on correcting the grammar in the prompt.
We will use nested if-else
statements inside the loop body.
This will get somewhat complicated.