【发布时间】: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