Source of BooleanDemo.java


  1: 
  2: import java.util.Scanner;
  3: 
  4: /**
  5:  Illustrates the use of a boolean variable to end loop iteration.
  6: */
  7: public class BooleanDemo
  8: {
  9:     public static void main(String[] args)
 10:     {
 11:         System.out.println("Enter nonnegative numbers.");
 12:         System.out.println("Place a negative number at the end");
 13:         System.out.println("to serve as an end marker.");
 14: 
 15:         int sum = 0;
 16:         boolean areMore = true;
 17:         Scanner keyboard = new Scanner(System.in);
 18: 
 19:         while (areMore)
 20:         {
 21:             int next = keyboard.nextInt( );
 22:             if (next < 0)
 23:                 areMore = false;
 24:             else
 25:                 sum = sum + next;
 26:        }
 27:            System.out.println("The sum of the numbers is " + sum);
 28:     }
 29: }
 30: