这是一个家庭作业问题.我将以下内容转换为递归函数时遇到问题:
public class Integrate {
public static double integrate(int a, int b, int steps)
{
double sum=0;
double delta = 1.0 * (b - a)/steps;
double x = a;
double f = 0.5*x*x + 3*x + 5;
for (int i = 0; i< steps; i++)
{
x = x + delta;
double fr = 0.5*x*x + 3*x + 5;
double area = f * delta + 0.5*(fr - f)*delta;
sum += area;
f = fr;
}
return sum;
}
public static void main(String [] args)
{
int a, b, step;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
step = Integer.parseInt(args[2]);
System.out.format("Integral is %f\n", integrate(a,b,step));
}
}
这是我到目前为止,但输出与原始代码不同.我无法弄清楚出了什么问题
public class Integrate {
public static double integrate(int a, int b, int steps) {
double sum=0;
int i=0;
sum = rintegrate(a, b, steps, i, sum);
return sum;
}
public static double rintegrate(int a, int b, int steps,
int i, double sum) {
double delta = 1.0 * (b - a)/steps;
double x = a;
double f = 0.5*x*x + 3*x + 5;
if (i
x = x + delta;
double fr = 0.5*x*x + 3*x + 5;
double area = f * delta + 0.5*(fr - f)*delta;
sum += area;
f = fr;
i++;
rintegrate(a, b, steps, i, sum);
}
return sum;
}
public static void main(String[] args) {
int a, b, step;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
step = Integer.parseInt(args[2]);
System.out.format("Integral is %f\n", integrate(a,b,step));
}
}