2
我想獲得在數字的0.000001範圍內舍入的數字的平方根。例如10的sqrt = 3.1622766 .....用雙。我有,但得到舍入爲3.162267是我遇到的問題。我必須使用循環,不能使用類。謝謝賈裏德得到一個數字來循環使用循環而不是Java中的類
import java.util.Scanner;
public class squareRoot {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a non-negative integer.");
int myInt = kb.nextInt();
{
double testNum;
double squareRoot = myInt/2;
do {
testNum = squareRoot;
squareRoot = (testNum + (myInt/testNum))/2;
}
while (squareRoot - (testNum * testNum) > .000001);
System.out.println("\nThe square root of " + myInt + " is " + squareRoot);
}
}
}
哇哦,這些缺口是真的* *混亂。 – Andreas