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:         int sum = 0;
 15:         boolean areMore = true;
 16:         Scanner keyboard = new Scanner(System.in);
 17:         while (areMore)
 18:         {
 19:             int next = keyboard.nextInt( );
 20:             if (next < 0)
 21:                 areMore = false;
 22:             else
 23:                 sum = sum + next;
 24:        }
 25:            System.out.println("The sum of the numbers is " + sum);
 26:     }
 27: }
 28: