我想編寫一個集成類,它會給我一個使用梯形法則的函數的積分。出於某種原因,我從來沒有代碼解析爲一個值,並保持停留在「總和+ =」步驟:無限循環代碼
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