Java 使用牛顿'确定平方根;s法

Java 使用牛顿'确定平方根;s法,java,while-loop,newtons-method,Java,While Loop,Newtons Method,这是一项家庭作业,使用牛顿法估算用户输入的数字的平方根,其结果应小于0.0001。当我运行代码并输入一个数字时,之后什么也不会发生。在调试模式下,“值”增加,这与我希望它做的相反。提前谢谢 import java.text.DecimalFormat; import java.util.Scanner; public class Newton { public static void main(String[] args) { // declare a

这是一项家庭作业,使用牛顿法估算用户输入的数字的平方根,其结果应小于0.0001。当我运行代码并输入一个数字时,之后什么也不会发生。在调试模式下,“值”增加,这与我希望它做的相反。提前谢谢

import java.text.DecimalFormat;
import java.util.Scanner;

public class Newton {

    public static void main(String[] args) 
      {
        // declare a Scanner class object
        Scanner sc = new Scanner(System.in);
        // declare a DecimalFormat class object
        DecimalFormat fourDecimal =  new DecimalFormat("0.0000");

        float Number = 0;

        System.out.println("Program: find square roots by Newton's Method");
        System.out.println("Please enter a number: ");

        Number = sc.nextFloat();

        System.out.println("The square root of " + Number + " is " + fourDecimal.format(Compute(Number)));
        }

    public static float Compute(float Number)
    {
    // define variable sqrRoot to hold the approximate square root
    float sqrRoot = 0;
    // define temporary variable temp to hold prior value of iteration
    float temp = 0;
    // divide variable num by 2 to start the iterative process
    // and assign the quotient to temp
    temp = Number/2;
    // open a while() loop that continues as long as num >= 0.0
    while (Number >= 0.0)
    {
    // construct the main iterative statement
        sqrRoot = temp - (temp * temp - Number) / (2 * temp);
    // open an if block to check if the absolute value of the difference of
    // variables temp and sqrRoot is below a small sentinel value such as 0.0001
    // if this condition is true then break the loop
        float value;
        value = Math.abs(temp - sqrRoot);
        if (value < .0001)
            // return sqrRoot as the answer
            Number = sqrRoot;
            // if this condition is not true then assign sqrRoot to temp
            else temp = sqrRoot;

    // close the while() loop
    }
    return Number;  
    }
}
导入java.text.DecimalFormat;
导入java.util.Scanner;
公共类牛顿{
公共静态void main(字符串[]args)
{
//声明扫描程序类对象
扫描仪sc=新的扫描仪(System.in);
//声明DecimalFormat类对象
十进制格式四进制=新的十进制格式(“0.0000”);
浮点数=0;
System.out.println(“程序:用牛顿法求平方根”);
System.out.println(“请输入一个数字:”);
编号=sc.nextFloat();
System.out.println(““+Number+”的平方根是“+fourDecimal.format(Compute(Number)));
}
公共静态浮点计算(浮点数)
{
//定义变量sqrRoot以保持近似平方根
浮点sqrRoot=0;
//定义临时变量temp以保存迭代的先前值
浮动温度=0;
//将变量num除以2开始迭代过程
//并将商分配给temp
温度=数字/2;
//打开一个while()循环,只要num>=0.0,该循环就会继续
而(数字>=0.0)
{
//构造主迭代语句
sqrRoot=温度-(温度*温度-编号)/(2*温度);
//打开if块以检查
//变量temp和sqrRoot低于一个小的sentinel值,如0.0001
//如果此条件为真,则断开循环
浮动值;
值=数学绝对值(温度-平方米);
如果(值<.0001)
//返回sqrRoot作为答案
数量=平方米;
//如果此条件不成立,则将sqrRoot分配给temp
否则温度=平方米;
//关闭while()循环
}
返回号码;
}
}

您的循环不会终止,因为您的条件是

while (Number >= 0.0)
如果您在满足条件时实际退出函数,则可以:

   if (value < .0001)
        // return sqrRoot as the answer
        return sqrRoot;
if(值<.0001)
//返回sqrRoot作为答案
返回序列号;
所以-改变最后一行,它就会工作

演示:

公共静态浮点计算(浮点数)
{
//定义变量sqrRoot以保持近似平方根
浮点sqrRoot=0;
//定义临时变量temp以保存迭代的先前值
浮动温度=0;
//将变量num除以2开始迭代过程
//并将商分配给temp
温度=数字/2;
//打开一个while()循环,只要num>=0.0,该循环就会继续

而(数字>=0.0)/工程师和学龄前儿童使用的巨大平方根表 到了20世纪70年代,牛顿法已经被一个五行程序所取代

public static Number squareRoot(Number value) {
    double temp = value.doubleValue() / 2;
    for(double sqrRoot; ; temp = sqrRoot) {
        sqrRoot = temp - (temp * temp - value.doubleValue()) / (2 * temp);
        if (Math.abs(temp - sqrRoot) < 1e-10) return sqrRoot;
    }
}
公共静态数字平方根(数值){
double temp=value.doubleValue()/2;
用于(双sqrRoot;;温度=sqrRoot){
sqrRoot=temp-(temp*temp-value.doubleValue())/(2*temp);
如果(数学abs(温度-sqrRoot)<1e-10)返回sqrRoot;
}
}

你有很多bug。我强烈建议你离开你的电脑,用铅笔和纸试着计算出你的
计算方法应该做什么。然后试着把它转换回Java。好的,我会的。谢谢。
public static Number squareRoot(Number value) {
    double temp = value.doubleValue() / 2;
    for(double sqrRoot; ; temp = sqrRoot) {
        sqrRoot = temp - (temp * temp - value.doubleValue()) / (2 * temp);
        if (Math.abs(temp - sqrRoot) < 1e-10) return sqrRoot;
    }
}