【问题标题】:Output Not Printing Everything?输出不打印所有内容?
【发布时间】:2014-09-25 05:41:39
【问题描述】:

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

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();
    }   

【问题讨论】:

  • 主要方法在哪里?
  • 您是否尝试过运行调试器来查看它挂起的位置?
  • 您期望输出是什么,输出是什么?如果你能把它变成一个简短但完整的程序也会有所帮助——我们应该能够将代码复制并粘贴到一个文件中,编译它,然后运行程序。您应该尝试将其简化为仍能证明问题的最小程序。
  • 您似乎遗漏了一些代码。请提供一个完整的示例,我们可以简单地复制和粘贴以自行编译和运行。此外,您应该提供一些示例输入和输出。

标签: java methods printing output call


【解决方案1】:

在您的newrap3() 方法中,变量diff 等于0,因此该方法返回而不打印输出的其余部分。

        diff = func3(x) / der3(x);
        if (diff == 0) {
            return;
        }

也许你想要一个break而不是return,就像这样:

        if (diff == 0) {
            break;
        }

【讨论】:

  • 有线猜测。你怎么知道这里的 main 方法是怎么用的?
  • @RuchiraGayanRanaweera,我认为的主要方法是刚开始时的前 3 个函数调用。
  • 在最后一个方法中将 return 更改为 break 会显示整个输出。谢谢!
【解决方案2】:

newrap3()i=22 中,x 的值为-1.4655712318767682

调用func3(-1.4655712318767682) 将返回0,所以diff = func3(x)/der3(x); 也将是0

然后if (diff == 0) return; 将在到达方法底部的 2 个打印语句之前退出 newrap3()

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    相关资源
    最近更新 更多