为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Key4042162 2017-04-13 21:46:49
此方程两个虚根为: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(); } }
查看完整描述

1 回答

已采纳
?
慕数据5846996

TA贡献1条经验 获得超1个赞


Math方法里的p有可能为负值,开根号的值不能为负

查看完整回答
1 反对 回复 2017-04-14
  • 1 回答
  • 0 关注
  • 1265 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信