1  import java.util.Scanner;
  2  
  3  /**
  4     This program prints the average of salary values that are terminated with a sentinel.
  5  */
  6  public class SentinelDemo
  7  {
  8     public static void main(String[] args)
  9     {  
 10        double sum = 0;
 11        int count = 0;
 12        double salary = 0;
 13        System.out.print("Enter salaries, -1 to finish: ");
 14        Scanner in = new Scanner(System.in);
 15  
 16        // Process data until the sentinel is entered 
 17  
 18        while (salary != -1)
 19        {  
 20           salary = in.nextDouble();
 21           if (salary != -1) 
 22           {  
 23              sum = sum + salary;
 24              count++;
 25           }
 26        }
 27  
 28        // Compute and print the average
 29  
 30        if (count > 0)
 31        {
 32           double average = sum / count;
 33           System.out.println("Average salary: " + average);
 34        }
 35        else
 36        {
 37           System.out.println("No data");
 38        }
 39     }
 40  }