Java 输出不是全部打印吗?

Java 输出不是全部打印吗?,java,methods,printing,output,call,Java,Methods,Printing,Output,Call,试着看看我的程序有什么问题。我不知道为什么输出没有一直打印,因为每个方法都是相同的,只是名称不同。它在接近终点的某个点后停止,但代码还有更多内容 public static void main(String[] args) { //x^3 + x^2 + 1 //2x^3 - 2x^2 - 2 //3x^3 + 3x^2 + 3 newrap1(); newrap2(); newrap3(); } public static dou

试着看看我的程序有什么问题。我不知道为什么输出没有一直打印,因为每个方法都是相同的,只是名称不同。它在接近终点的某个点后停止,但代码还有更多内容

public static void main(String[] args)
{       
    //x^3 + x^2 + 1
    //2x^3 - 2x^2 - 2
    //3x^3 + 3x^2 + 3

    newrap1();
    newrap2();
    newrap3();
}
public static double func1(double x)
    {
        double f1;
        f1 = Math.pow(x, 3) + Math.pow(x, 2) + 1;
        return f1;
    }
public static double func2(double x)
    {
        double f2;
        f2 = 2*Math.pow(x, 3) - 2*Math.pow(x, 2) - 2;
        return f2;
    }
public static double func3(double x)
    {
        double f3;
        f3 = 3*Math.pow(x, 3) + 3*Math.pow(x, 2) + 3;
        return f3;
    }
public static double der1(double x)
    {
        double d1;
        d1 = 3*Math.pow(x, 2) + 2*x;
        return d1;
    }
public static double der2(double x)
    {
        double d2;
        d2 = 6*Math.pow(x, 2) - 4*x;
        return d2;
    }
public static double der3(double x)
    {
        double d3;
        d3 = 9*Math.pow(x, 2) + 6*x;
        return d3;
    }
public static void newrap1()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func1(x)/der1(x);
            if (diff == 0) return;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);

        }
        System.out.println("The root is -1.465572 after 20 iterations.");
        System.out.println();
    }
public static void newrap2()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func2(x)/der2(x);
            if (diff == 0) return;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);
        }
        System.out.println("The root is 1.465571 after 15 iterations.");
        System.out.println();
    }
public static void newrap3()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func3(x)/der3(x);
            if (diff == 0) return;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);
        }
        System.out.println("The root is -1.465572 after 20 iterations.");
        System.out.println();
    }   
在newrap3方法中,变量diff等于零,因此该方法返回时不打印其余的输出

        diff = func3(x) / der3(x);
        if (diff == 0) {
            return;
        }
也许你不想回来,而是想休息一下,就像这样:

        if (diff == 0) {
            break;
        }
在i=22时的newrap3中,x的值为-1.4655712318767682

调用func3-1.4655712318767682将返回0 so diff=func3x/der3x;也将是0


如果diff==0,则返回;将在到达方法底部的2条打印语句之前退出newrap3。

主方法在哪里?是否尝试运行调试器以查看其挂起的位置?请提供一个。您希望输出是什么,输出是什么?如果你能把它变成一个简短但完整的程序,它也会有所帮助——我们应该能够将代码复制并粘贴到文件中,编译并运行程序。你应该试着把它简化成一个仍然能演示问题的最小程序。看起来你遗漏了一些代码。请提供一个完整的例子,我们可以简单地复制和粘贴编译和运行自己。此外,还应提供一些示例input和output.wired guess。你怎么知道main方法在这里是如何使用的?@RuchiraGayanRanaweera,我假设它是刚开始的前3个函数调用的main方法。在最后一个方法中将return改为break显示了整个输出。谢谢