2013-10-11 36 views
1

我在寫一個java程序來最終求解一個多項式。現在我只是試圖在表格中打印出x和y值。但是,一旦我的程序運行完畢後我得到這個錯誤:Java多項式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; 
    } 
} 

回答

2

只看該快速

double[] yValues = new double[12]; 

for (int t=0; t<=12; t++){ 

要麼做出

double[] yValues = new double[13]; 

for (int t=0; t<12; t++){ 

(注意< =改爲<)

另外,把{}放在你的for(double ii:equ)循環中。你只是在問問題。其次,修復你的凹痕

2

創建陣列

double[] yValues = new double[12]; 

這意味着yValues有從0到11

指數但你做

for (int t=0; t<=12; t++){ 

包括值12和最後

yValues[t] = sum; 
1

問題是yValues被初始化與從0到11的索引12個字段,但要從0迭代至12,它們是13號。將第一個for循環equParts更改爲

for (int t=0; t<12; t++) 

它應該工作。

相關問題