The complete program is given below.
To complete the program, add one to count
as the documentation
suggests.
Notice how the three variables
value
, sum
, and count
work together.
Often in programming,
you need to coordinate
several variables.
Let us work on improving the grammar in the prompt.
We will use nested if-else
statements inside the loop body.
int value; // data entered by the user int sum = 0; // initialize the sum int count = 0; // number of integers read in // get the first value System.out.println( "Enter first integer (enter 0 to quit):" ); value = scan.nextInt(); 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):" ); value = scan.nextInt(); } System.out.println( "Sum of the " + count + " integers: " + sum ); } }
What suffix goes with each integer? E.g. 1 gets "st" to make 1st.