需要幫助... 我試着運行梯形法則的代碼。這是我在數值方法的項目,下面的代碼:在梯形規則上需要幫助Java代碼
static double trapezoidRule (int size, double[] x, double[] y)
{ double sum = 0.0,
increment;
for (int k = 1; k < size; k++)
{//Trapezoid rule: 1/2 h * (f0 + f1)
increment = 0.5 * (x[k]-x[k-1]) * (y[k]+y[k-1]);
sum += increment;
}
return sum;
}
public static void main (String[] args) throws Exception
{ String fileName = args.length > 0 ? args[0] : "InpData.txt";
Scanner inp = new Scanner(new File(fileName));
int k, size;
double[] x, y;
double integral;
size = inp.nextInt();
System.out.println ("Number of points: " + size);
x = new double[size];
y = new double[size];
for (k = 0; k < size; k++)
{ x[k] = inp.nextDouble();
y[k] = inp.nextDouble();
}
integral = trapezoidRule (size, x, y);
System.out.printf ("Integral: %4.4f\n", integral);
System.out.printf ("Check: log(%2.2f) = %8.8f\n",
x[size-1], Math.log(x[size-1]));
}
}
它不能編譯,我總是得到FileNotFoundException異常。我在Javadocs上發現,當具有路徑名的文件不存在時,這將被拋出。請幫忙。謝謝!
這不是編譯錯誤,在運行時拋出FileNotFoundException。 – 2014-12-03 09:53:18