A good answer might be:

See below.

for Loop Version

The for loop version also seems slighly awkward. You have to remember that the "change" part of the for statement can be omitted. This is correct syntax, but now you must be careful to make the change in the loop body.

import java.io.* ;
class SqrtCalc
{
  public static void main( String[] args )
      throws IOException
  {
    String chars;
    double x;
    BufferedReader stdin = new BufferedReader( 
        new InputStreamReader(System.in) );

    for ( chars = "yes"; chars.equals( "yes" );  )  // missing last part,
    {                                               // but this is OK
      System.out.print("Enter a number-->");
      chars = stdin.readLine(); 
      x     = (Double.valueOf(chars)).doubleValue();
      System.out.println("Square root of" + x +
          " is " + Math.sqrt( x ) );
      System.out.print("Do you wish to continue? (yes or no) -->");
      chars = stdin.readLine(); 
    }

  }
}

Of course, the biggest problem with all three versions is that the user must type exactly "yes" for the program to continue. Better programs would allow "y" or "Y" or "YES" .

QUESTION 7:

You want the program to loop again when the user enters:

  • yes
  • YES
  • Y
  • y
and to quit for whatever else the user enters. How (in general) can you do this?

Click Here after you have answered the question