早教吧 育儿知识 作业答案 考试题库 百科 知识分享

JAVA实现牛顿迭代求任意非负数的二次根

题目详情
JAVA实现牛顿迭代求任意非负数的二次根
▼优质解答
答案和解析
import java.util.Scanner;
public class Newton {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("输入:");
double a = scan.nextDouble();
double x = 1;
double y = 0;
while (true) {
y = x - (x * x - a) / (2 * x);
if (Math.abs((x - y)) < 1e-6) {
break;
}
x = y;
}
System.out.println("结果:" + x);
}
}