import java.util.Scanner;
public class Homework0317Part1
{
public static void main(String[] args)
{
System.out.println("please enter a number.");
Scanner keyboard = new Scanner(System.in);
double n=keyboard.nextDouble();
double myGuess=n/2;
double r=0.0;
for(int i=0;i<5;i++)
{
r=n/myGuess;
myGuess=(myGuess+r)/2;
}
System.out.print("my guess is ");
System.out.printf("%10.2f",myGuess);
System.out.println();
System.out.print("the real one is ");
System.out.printf("%10.2f",Math.sqrt(n));
}
}
結果:
1.使用較小的數字(誤差較小)
2.使用較大的數字(誤差較大)
沒有留言:
張貼留言