您的位置首页百科问答

用迭代法求x=根号a,这个错在哪里?

用迭代法求x=根号a,这个错在哪里?

public static void main(String[] args){double a, x1, x2, b = 1;Scanner sc=new Scanner(System.in);a=sc.nextDouble();x1 = a / 2;while ( b >= 1e-5){x2 = (x1 + a / x1) / 2;b = Math.abs(x1 - x2);x1 = x2;}System.out.println(x1);}