我收到运行时错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Series.series(Series.java:10)
at Series.main(Series.java:21)
在第 10 行和第 21 行中,无法弄清楚出了什么问题。我正在尝试使程序对系列 1 - 2x + 3x^2 + 4x^3 ... n*x^(n-1) 求和。我能得到的任何帮助将不胜感激!
public class Series {
public static double series (double x, int n) {
int increase = n;
double sign = Math.pow(-1.0, increase+1);
double exponent = Math.pow(x, increase-1);
double[] A = new double[n];
for (int i = 0; i <= n; i++) {
A[i] = (sign) * ((increase + 1) - A.length) * (exponent); increase = increase + 1;
}
double sum = 0;
for (int i = 0; i < A.length; i++) {
sum = sum + A[i];
}
return sum;
}
public static void main (String[] args) {
System.out.print("series(0.5, 1) should be 1.0");
System.out.println(" : " + series(0.5, 1));
}
}