See below.
while
Loop Version
The first iteration of the loop body
can be made to happen by initializing
chars
to "yes."
This is slighly awkward.
import java.util.Scanner; class SqrtCalc { public static void main( String[] args ) { String chars; double x; Scanner scan = new Scanner(System.in ); chars = "yes" ; // enable first iteration of the loop while ( chars.equals( "yes" ) ) { System.out.print("Enter a number-->"); x = scan.nextDouble(); chars = scan.nextLine(); // flush rest of the line System.out.println("Square root of " + x + " is " + Math.sqrt( x ) ); System.out.print("Do you wish to continue? (yes or no) --> "); chars = scan.nextLine(); } } }
Examine the code (again.) How would it be written with a
for
loop?