2015-02-10 74 views
0

我正在写一个算法计算3次多项式的根,算法崩溃。我将整个代码复制到NetBeans,并且工作正常。下面是Android Studio中的代码:因变量声明导致应用程序崩溃

public static double[] PolySolver(double c3,double c2,double c1,double c0){ 
    double[] roots=new double[3]; 
    int k=0; 
    roots[0]=258.74; 
    roots[1]=258.74; 
    roots[2]=258.74; 
    double x0=-1; 
    double x1=1; 
    for(int i=0; i <c0; i++){ 
     double Rawroot=SecantMethod(c3,c2,c1,c0,x0,x1); 
     String sroot=""+Rawroot; 
     if(sroot.length()>8){ 
      sroot=sroot.substring(0, 8); 
     } 
     double root=Double.parseDouble(sroot); 
     if(k<=2&& root!=roots[2]&&root!=roots[1]&& root!=roots[0]){ 
      roots[k]=root; 
      if(k==2){ 
       return roots; 
      } 
      k++; 

     } 

     x0=x0-1; 
     x1=x1+1; 
    } 
    return roots; 
} 


public static double SecantMethod(double c3,double c2,double c1,double c0,double x0,double x1) 
{ 
// Local variables 
     double x, // Calculated value of x at each iteration 
       f0, // Function value at x0 
       f1, // Function value at x1 
       fx, // Function value at calculated value of x 
       root, // Root, if within desired tolerance 
       // x0=-1, //First guess 
       //x1=c0, //second guess 
       tol=0.000000001, //accuracy 
       n=2000; //number of times executed 
// Set initial function values 
     f0 = Function(c3,c2,c1,c0,x0); 
     f1 = Function(c3,c2,c1,c0,x1); 
// Loop for finding root using Secant Method 
     for(int i = 0; i < n; i++) 
     { 
      x = x1 - f1 * ((x1 - x0)/(f1 - f0)); 
      fx = Function(c3,c2,c1,c0,x); 
      x0 = x1; 
      x1 = x; 
      f0 = f1; 
      f1 = fx; 
// Check whether calculated value is within tolerance 
      if(Math.abs(x1 - x0) < tol) 
      { 
       root = x1; 

       return root; 
      } // end if 
     } // end for 
     return x1; 
    } 
    public static double Function(double c3, double c2, double c1,double c0,double x){ 
     double y=c3*(Math.pow(x, 3))+c2*(Math.pow(x, 2))+c1*x+c0; 
     return y; 
    } 

} 

我测试一切,似乎线路导致应用:

public class poly3_next extends Activity{ 

//public final static String EXTRA_MESSAGE = "com.michniewicz.jan.mathcalcalpha.poly3_next"; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_poly3_next); 


    //getting data from previous activity 

    String value=""; 
Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      value = extras.getString("pass"); 
     } 
     final String finalValue = value; 



    final TextView display1 = (TextView) findViewById(R.id.poly3_next_tbe1); 
    final TextView display2 = (TextView) findViewById(R.id.poly3_next_tbe2); 
    final TextView display3 = (TextView) findViewById(R.id.poly3_next_tbe3); 

    String[] coefficients=new String[4]; 
    int k=0; 
    int pointer2=0; 
    for(int pointer=0; pointer<finalValue.length();pointer++){ 

     if((finalValue.substring(pointer,pointer+1)).equals("@")){ 
      coefficients[k]=finalValue.substring(pointer2,pointer); 
      pointer2=pointer+1; 
      k++; 
     } 
    } 



    double cf0=Double.parseDouble(coefficients[3]); 
    double cf1=Double.parseDouble(coefficients[2]); 
    double cf2=Double.parseDouble(coefficients[1]); 
    double cf3=Double.parseDouble(coefficients[0]); 

    double solution1=0,solution2=0,solution3=0; 



    solution1=PolySolver(cf3,cf2,cf1,cf0)[0]; 
    display1.setText(""+solution1); 
    solution2=PolySolver(cf3,cf2,cf1,cf0)[1]; 
    display2.setText(""+solution2); 
    solution3=PolySolver(cf3,cf2,cf1,cf0)[2]; 
    display3.setText(""+solution3); 


     } 

解决工作正常,但我会后他们以防万一的功能崩溃是:

double cf0=Double.parseDouble(coefficients[3]); 
     double cf1=Double.parseDouble(coefficients[2]); 
     double cf2=Double.parseDouble(coefficients[1]); 
     double cf3=Double.parseDouble(coefficients[0]); 
+0

你能为我们提供一个堆栈跟踪? – 2015-02-10 16:16:25

+1

为什么人们恨【型号】.valueOf()这么多? – LokiSinclair 2015-02-10 16:21:11

+0

,因为它[多一个电话](https://github.com/justinsb/android-libcore/blob/master/luni/src/main/java/java/lang/Double.java#L331)(取决于JIT最优化) – Selvin 2015-02-10 16:29:35

回答

0

尝试改变这一点:

for(int pointer=0; pointer<finalValue.length();pointer++){ 

    if((finalValue.substring(pointer,pointer+1)).equals("@")){ 
     coefficients[k]=finalValue.substring(pointer2,pointer); 
     pointer2=pointer+1; 
     k++; 
    } 
} 

if你写finalValue.substring(pointer, pointer+1).equal... 评论这条线并尝试运行。 它在过去的循环,因为坠毁指针+ 1的值是空的。

例如:

for(max 5 time){ 
    substring(0, 1) 
    substring(1, 2) 
    .... 
    substring(5, 6!) 
} 
相关问题