import java.util.*; public class Average { public static void main(String [] args) { // 1. declare one variable for input // 2. initialize the sum // 3. read an input from the user // 4. if it is not equal to -999, add to the sum // 5. repeat steps 3 and 4 until -999 is entered // 6. take the average int num; // An integer entered by the user int sum; // The sum of all integers int counter; double average; Scanner con; con = new Scanner(System.in); counter = 0; sum = 0; System.out.println("Enter a positive integer, (-999 marks the end of input)"); num = con.nextInt(); // While loop is used to repeatedly take an input from the user while (num != -999) { // Loop body sum = sum + num; // Prompt the user enter another integer System.out.println("Enter a positive integer, (-999 marks the end of input)"); num = con.nextInt(); // Update the counter counter++; } if (counter > 0) { average = sum / counter; System.out.println("The average is " + average); } else { // avoid the division by 0 System.out.println("No positive integer has been entered."); } } }