if (nextGuess = lastGuess + (num / lastGuess) / 2
^
illegal start of type
else if (nextGuess - lastGuess) < 0
illegal start of expression
break;
^
3 errors
Tool completed with exit code 1
Here's my code:
import javax.swing.JOptionPane;
public class SquareRootCalculatorc
{
// main method, starting point of execution
public static void main (String [] args)
{
// read input from the user
String numString = JOptionPane.showInputDialog("Enter number, for example 2.0;");
double num = Double.parseDouble(numString);
// store input in a variable
double num = lastGuess;
// call square root method here
double result = calculateSquareRoot(number);
// display square root of number entered by the user
System.out.println("The square root of " + number + " is " + result);
// compare your result withe the Math.sqrt() method
}
// sqaure root method
public static double calculateSquareRoot(double num)
{
// this is the procedure shown in the programming exercise
// initialize last guess to 1.0
double lastGuess = 1.0;
// initialize next guess to 0.0
double nextGuess = 0.0;
// loop for ever
// for (;; )
while (true) // this is easier to understand
{ System.out.print("\nEnter your number:");
double num = input.nextDouble();
// calculate next guess based on current value of last guess
// use the formula shown in the exercise
if (nextGuess = lastGuess + (num / lastGuess) / 2
System.out.println("Yes, the square root is" + result);
else if (nextGuess - lastGuess) < 0
// if difference between next and last guess is less than 0.0001
// exit loop
break;
}
// otherwise
// assign next guess to last guess
// outside of loop, return next guess
}
}
Any advice? Thanks.

New Topic/Question
Reply



MultiQuote







|