Source of BooleanDemo.java


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