я получаю сообщение об ошибке выполнения:я продолжаю получать исключение в потоке ошибки «основной» java.lang.ArrayIndexOutOfBoundsException
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));
}
}
Является ли это (1 - 2x + 3x^2 + 4x^3 ... п * х^(п-1)) или (1+ 2x + 3x^2 + 4x^3 ... n * x^(n-1)) –
Обычно условия цикла должны использовать '<', а не '<='. –