我正在编写一个计算3度多项式根的算法,我的App崩溃了。我将整个代码复制到NetBeans,它工作正常。这是android studio的代码:
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);
}
解决工作正常的功能,但我会发布以防万一:
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;
}
}
我测试了所有内容,似乎导致应用崩溃的行是:
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 :(得分: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!)
}