DoLoop.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program shows how you can prompt for input that fulfills
  5     a certain condition. Try entering negative values, such as
  6     -2 or -0.5, to see how they are rejected.
  7  */
  8  public class DoLoop 
  9  {
 10     public static void main(String[] args)
 11     {
 12        Scanner in = new Scanner(System.in);
 13        
 14        double value;
 15        do
 16        {
 17           System.out.print("Enter a number >= 0: ");
 18           value = in.nextDouble();
 19        }
 20        while (value < 0);
 21        double root = Math.sqrt(value);
 22        System.out.println("The square root of the number is " + root);
 23     }
 24  }