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, but 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 getting the English grammar in the prompt correct. This will involve putting nested if-else statements inside the loop body. This will get somewhat complicated.