Source of BooleanDemo.java


  1: 
  2: import java.util.*;
  3: 
  4: /**
  5:  Illustrates the use of a boolean variable to control loop ending.
  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 next, sum = 0;
 15:         boolean numbersLeft = true;
 16:         Scanner keyboard = new Scanner(System.in);
 17:         while (numbersLeft)
 18:         {
 19:             next = keyboard.nextInt( );
 20:             if (next < 0)
 21:                  numbersLeft = false;
 22:             else
 23:                  sum = sum + next;
 24:        }
 25:         System.out.println("The sum of the numbers is " + sum);
 26:     }
 27: }
 28: