## CSDN博客

### 简单小程序 求一元2次方程的根

import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.applet.Applet;
public class qiugen  extends Applet implements ActionListener
{

TextField  test1,test2,test3,test4,test5;
double a,b,c;
double x1,x2;
Button mybutton;
public void init()
{
test1=new TextField(2);
test2=new TextField(2);
test3=new TextField(2);
test4=new TextField(4);
mybutton=new Button("计算");
test5=new TextField(50);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mybutton)
{
a=Double.parseDouble(test1.getText());
b=Double.parseDouble(test2.getText());
c=Double.parseDouble(test3.getText());
test4.setText(String.valueOf(b*b-4*a*c));
if(b*b-4*a*c>=0&&a!=0)
{
x1=(-b+java.lang.Math.sqrt(b*b-4*a*c))/(2*a);
x2=(-b-java.lang.Math.sqrt(b*b-4*a*c))/(2*a);
test5.setText("根x1="+x1+"  根 x2="+x2);

}
else if(a==0)
{
x1=x2=-c/b;
test5.setText("根x1="+x1+"  根 x2="+x2);
}
else if(b*b-4*a*c<=0)
{
x1=java.lang.Math.sqrt(4*a*c-b*b)/(2*a);

test5.setText("根x1="+b/(2*a)+"+"+String.valueOf(x1)+"i"+"  x2="+(-b/(2*a))+"+"+String.valueOf(x1)+"i");
}
}
}
}

<meta http-equiv="Content-Type" content="text/html; charset=??????">
<title>一个简单的程序</title>