我想编写一个集成类,它会给我一个使用梯形法则的函数的积分。出于某种原因,我从来没有代码解析为一个值,并保持停留在“总和+ =”步骤:无限循环代码
public static double Trap(int exponent, int a, int b, int n) {
double h = (double) (b-a)/n;
double sum = 0;
for(int i = a; i <= b; i+=h)
sum += (Math.pow(i, exponent) + Math.pow(i+1, exponent))/2; //trouble!
return h * sum;
}
public static void main(String[] args) {
System.out.println(Trap(3,1,3,10)); //integral of x^3 from 1 to 3,
divided into 10 parts
}
不要试图“大胆”的代码。改为使用评论。 – 2013-03-26 01:45:36
我可以想象'h'总是0. – Sebastian 2013-03-26 01:48:22
明白了。把所有东西都改为双重 – Haque1 2013-03-26 01:52:34