java解一元二次方程 运行出错?

此方程两个虚根为:NaNi,NaNi

这是怎么回事???(编译没问题 运行出上面的语句)

class Equation{

 private float a,b,c;

 static double z,p;

     public Equation(){}

     public Equation(float a,float b,float c){

  this.a=a;

  this.b=b;

  this.c=c;

}

    public void setA(float a){

  this.a=a;

}

    public float getA(){

     return a;

}

    public void setB(float b){

     this.b=b;

}

    public float getB(){

     return b;

}

    public void setC(float c){

     this.c=c;

}

    public float getC(){

     return c;

}

 

 public void Math(){

      p=b*b-4*a*c;

      z=Math.sqrt(p);

 }

 

 private boolean panduan(){

  return z!=0;

 }

 private boolean panduan2(){

  return z>0;

 }

 public void showEquation(){

  double f,g;

  f=-b+z;g=-b-z;

  if(panduan()){

    if(panduan2()){

     System.out.println("此方程两个实根为:"+f/(2*a)+","+g/(2*a));

    }else{

     System.out.println("此方程两个虚根为:"+g/(2*a)+"i,"+f/(2*a)+"i");

    }

   }else{

   System.out.println("此方程两个相同的实根为:"+(-b+z)/(2*a));

  }

}

}

class TestEquation{

 public static void main(String args[]){

 Equation a1 = new Equation(5.0f,3.0f,2.0f);

  a1.Math();

  a1.showEquation();

}

}

Key4042162
浏览 1267回答 1
1回答

慕数据5846996

Math方法里的p有可能为负值,开根号的值不能为负
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java