当判别式<0,这个时候就产生虚根了,写起来比较麻烦,算法是这样的
举个简单例子说明吧:
求一元二次方程:x^2+x+1=0的虚根
解:△=1^2-4×1×1=-3
由求根公式x=[-b±√(-△)i]/2a =[-1±√(-△)i]/2=[-1±√3i]/2
3、NAN这个我写一下它的全称你就知道了no a number就是运算结果不是数字,如果你将0作为除数,就能得到这个结果
你的程序我修改了一下
import java.io.*;
class wp{
private double a,b,c,realpart,imagpart,disc;
BufferedReader s1 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader s2 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader s3 = new BufferedReader(new InputStreamReader(System.in));
wp(){
try{
a=Double.valueOf(s1.readLine()) ;
b=Double.valueOf(s2.readLine()) ;
c=Double.valueOf(s3.readLine()) ;
}
catch(Exception e)
{
System.out.println("输入错误!!!") ;
}
}
void solve(){
realpart=-(b/(2*a));
System.out.println(realpart) ;
imagpart=(Math.sqrt(b*b-4*a*c)/(2*a));
System.out.println(imagpart) ;
if(Math.abs(a)<=0.000001){
System.out.println("Is not a quadratic!");
}
else{
System.out.println("It is a quadratic");
disc=b*b-4*a*c;
if(Math.abs(disc)<=0.0000001){
System.out.println("has two equal roots="+(realpart+imagpart));
}
else if(disc>0.0000001)
System.out.println("has distinct real roots="+(realpart+imagpart)+"+"+(realpart-imagpart));
else
System.out.println("has complex roots=");
System.out.print("x1 = "+realpart+"+"+(Math.sqrt(-(b*b-4*a*c))/(2*a)+"i")) ;
System.out.print(" x2="+realpart+"-"+(Math.sqrt(-(b*b-4*a*c))/(2*a)+"i")) ;
}
}
public static void main(String[] args){
wp w=new wp();
w.solve();
}
}