cacaopie 于 2015.05.27 22:21 提问

7个回答

caozhy      2015.05.27 23:27
u013238950   2015.05.28 00:33

public class demo{
public static void main(String[] str){
double a1 = 1, b1 = 2;

``````  double x1 = 1;
double x2 = (a1 + b1) / 2;
//c存储前一个x2值
double c = 0 ;
double check = 1;
while(check >= 0.00005){
x2  = (a1 + b1)/2.0;
check = Math.abs(x2 - c);
double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10;
if(result > 0){
b1 = x2; a1 = x1; c = x2;
}
else {
a1 = x2;
c = x2;
}

}

System.out.println(x2);
}
``````

}

1.3652290927316866

u013238950 回复cacaopie:
2 年多之前 回复
cacaopie 谢谢了啊！可是为什么是1.36522...啊？和答案不一样啊？你这个程序也启发了我一点，你看看我这个程序哪里错了啊...有点乱不好意思啊...
2 年多之前 回复
cacaopie public static void main(String[] args) {double a,b,c,d,A=1.0,B=2.0; a=Math.pow(A, 3) + 4 * Math.pow(A, 2) - 10; b=Math.pow(B, 3) + 4 * Math.pow(B, 2) - 10; c=(B-A)/2+A; d=Math.pow(c, 3) + 4 * Math.pow(c, 2) - 10do { if(a*d>=0){A=c;B=2; a=A*A*A+4*A*A-10; b=B*B*B+4*A*A-10; c=(B-A)/2+A;d=c*c*c+4*c*c-10;}else{A=1;B=c;a=A*A*A+4*A*A-10;b=B*B*B+4*A*A-10;c=(B-A)/2+A; d=c*c*c+4*c*c-10; } } while((B-A)/2<0.0005); System.out.printf("%f\n",d); } }
2 年多之前 回复
u013238950   2015.05.29 08:24

u013238950   2015.05.29 08:30

public class demo {

public static void main(String[] str) {

double a1 = 1, b1 = 2;

``````    double x1 = 1;
double x2 = (a1 + b1) / 2;
// c存储前一个x2值
double c = 0;
while (Math.abs(x2 - c) >= 0.00005) {

double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10.0;
if (result > 0) {
b1 = x2;
a1 = x1;
c = x2;
} else {
a1 = x2;
c = x2;
}
x2 = (a1 + b1) / 2.00;
}
System.out.println(x2);
}
``````

}

``````
``````
``````
``````
``````
``````
``````
``````
u013238950   2015.05.29 08:34

cacaopie 好像就是哈...嘿嘿...我再看看，谢谢了啊！
2 年多之前 回复
u013238950   2015.05.29 08:43

u013238950   2015.06.01 20:41

public static void main(String[] args) {
double a1 = 1.0, b1 = 2.0;

``````        double x1 = 1.0;
double x2 ;
while (Math.abs(b1 - a1) >= 0.0005) {
x2 = (a1 + b1) / 2;
double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10.0;
if (result > 0) {
b1 = x2;

} else {
a1 = x2;
}

}
System.out.println(a1);
}
又仔细看了一下，发现以前理解错了，这次输出是1.36474609375
``````