首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

递归调用 函数调用次数有关问题!

2013-06-19 
递归调用 函数调用次数问题!!package 实验一import java.util.Scannerpublic class Test1 {static doubl

递归调用 函数调用次数问题!!




package 实验一;

import java.util.Scanner;


public class Test1 {

static double EPS;
static int I=1;    //二分次数

public static double f(double x) {
return x * x * x + x * x - 3 * x - 3;
}

public static double bisection(double a, double b) {
//System.out.println(I);

double fab = f(a) * f(b);
if (fab >= 0) {
System.out.println("该区间无根!");
return 0;
} else {

double c = (a + b) / 2;

if (f(a) * f(c) < 0) {
if (c - a < EPS) {
return c;
} else {
I=I+1;
return bisection(a, c);
}
} else {
if (b - c < EPS) {
return c;
} else {
I=I+1;;
return bisection(c, b);
}
}
}
}

public static void main(String[] args) {
//System.out.println(bisection(1,2));
System.out.print("请输入单根区间[a,b]中a点的值:");
double a=KB.scanDouble();
System.out.print("\n请输入单根区间[a,b]中b点的值:");
double b=KB.scanDouble();
System.out.print("\n请输入根的容许误差限EPS的值:\n");
EPS=KB.scanDouble();
System.out.println("二分次数为:"+I);
System.out.println("所得到的根为:"+bisection(a,b));
}

}




class KB { 

  public static double scanDouble(){   //接收数值输入 
  Scanner in=new Scanner(System.in); 
      return in.nextDouble();
  } 
}




这是二分法求根的小程序!I表示调用次数,EPS表示误差允许值!!得到的结果永远是1,错的!求大家帮我看看哪里错了!!


运行结果如下:


请输入单根区间[a,b]中a点的值:1

请输入单根区间[a,b]中b点的值:2

请输入)根的容许误差限EPS的值:
5e-6
二分次数为:1
所得到的根为:1.7320518493652344
Java 递归
[解决办法]
要把基本运算是什么搞清楚。。楼主你回头看看自己这么放的话基本运算成啥了

热点排行