import java.awt.*;
import java.applet.*;
import java.text.DecimalFormat;
/*
<TITLE>Example</TITLE>
<applet code="Experiment1" width=500 height=200>
</applet>
*/
public class Experiment1 extends Applet{
DecimalFormat bd=new DecimalFormat("0.0000");//把double四舍五入取整
String str1,str2;
public void init(){
setBackground(Color.red );
setForeground(Color.cyan);
}
void Solving(double a,double b,double c){
double x1,x2;
double d=b*b-4*a*c;
if(d>0){
x1=(-b+Math.sqrt(d))/(2*a);
x2=(-b-Math.sqrt(d))/(2*a);
str1=""+(int)a+"x2+"+(int)b+"x+"+(int)c+"=0 have two different real roots";
str2="x1="+bd.format(x1)+";x2="+bd.format(x2);
}
else if(d==0){
x1=-b/(2*a);
str1=""+(int)a+"x2+"+(int)b+"x+"+(int)c+"=0 have one real root";
str2="x="+bd.format(x1);
}
else{
x1=-b/(2*a);
x2=Math.sqrt(-d)/(2*a);
str1=""+(int)a+"x2+"+(int)b+"x+"+(int)c+"=0 have two different Imaginary roots";
str2="x1="+bd.format(x1)+"+"+bd.format(x2)+"i;x2="+bd.format(x1)+"-"+bd.format(x2)+"i";
}
}
public void paint(Graphics g){
Solving(1,5,3);
g.drawString(str1,20,20);
g.drawString(str2,20,40);
Solving(4,4,1);
g.drawString(str1, 20,70);
g.drawString(str2, 20,90);
Solving(2,3,2);
g.drawString(str1, 20,120);
g.drawString(str2, 20,140);
showStatus("This is a example of solving x");
}
}
Java Application和Applet两种方式求一元二次方程ax2+bx+c=0的根
5星 · 超过95%的资源 需积分: 35 49 浏览量
2011-12-22
09:58:25
上传
评论 4
收藏 5KB ZIP 举报
fanfangogo90
- 粉丝: 2
- 资源: 8