Я пишу программу Java, чтобы в конечном итоге решить многочлен. На данный момент я просто пытаюсь распечатать значения x и y в таблице. Но, как только моя программа закончит работу, я получаю эту ошибку:Java Polynomial ArrayIndexOutOfBoundsException
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at mathassignment1pt2.MathAssignment1Pt2.equParts(MathAssignment1Pt2.java:82)
at mathassignment1pt2.MathAssignment1Pt2.main(MathAssignment1Pt2.java:50)
Java Результат: 1
Линия 82 является: yValues[t] = sum
;
Линия 50: MathAssignment1Pt2.yValues = equParts(MathAssignment1Pt2.cofs, xValues, MathAssignment1Pt2.deg);
Вот мой код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mathassignment1pt2;
import java.util.Scanner;
import java.lang.Math.*;
import java.lang.Math.*;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author thomasbuss
*/
public class MathAssignment1Pt2 {
public static double[] cofs;
public static int deg;
public static double[] yValues;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
System.out.print("Enter Degree: ");
deg = in.nextInt();
cofs = new double[deg+1];
int counter = 1;
for (int i = 0; i<deg+1; i++){
System.out.print("You want coefficient " + counter + " to be: ");
cofs[i] = in.nextDouble();
counter++;
}
System.out.println("Your estimated number of roots is: " + deg);
double rmax = newtons(cofs[0], cofs[1], cofs[2]);
double negrmax = -rmax;
double[] xValues = new double[]{-3,-2.5,-2,-1.5,-1,-.5,0,.5,1,1.5,2,2.5,3};
MathAssignment1Pt2.yValues = equParts(MathAssignment1Pt2.cofs, xValues, MathAssignment1Pt2.deg);
System.out.println("X-Values" + "\t");
System.out.print("Y-Values" + "\n");
for (int printer = 0; printer<=12; printer++){
System.out.println(xValues[printer] + "\t");
System.out.print(MathAssignment1Pt2.yValues[printer] + "\n");
}
}
public static double newtons(double a, double b, double c){
double solveNewtons = Math.sqrt(Math.pow((b/a), 2) - 2*(c/a));
return solveNewtons;
}
public static double[] equParts(double[] cofs, double[] x, int deg){
double[] equ = new double[deg];
double[] yValues = new double[12];
for (int t=0; t<=12; t++){
for (int i=0; i<=deg; i++){
equ[i] = cofs[i]*Math.pow(x[t], deg);
deg--;
}
double sum = 0;
for (double ii : equ)
sum += ii;
sum += cofs[deg+1];
yValues[t] = sum;
}
return yValues;
}
}