求解一元二次多项式的根

 a*X*X+b*X+c=0

将不同的abc值硬编码到程序中,测试你的程序,观察输出的结果。如:
a = 1, b = 2, c = 0;
a = 0 ,b = 1, c = 1;
a = 1, b = 2, c = 1
……

要求用Java eclipse 来编写。

 public class Test3 { //一元二次多项式求根 public static void Root(int a, int b, int c) { if(a == 0) { double x; if(b == 0) { if(c == 0) { System.out.println("这个一元二次多项式的根x为任意实数。"); } else { System.out.println("这个一元二次多项式无实数解。"); } } else { x = -c/b; System.out.println("这个一元二次多项式有一个实根为:x = "+x); } } else { double x1, x2, temp; if(b * b - 4 * a * c == 0) { x1 = x2 = -b/(2 * a); System.out.println("这个一元二次多项式有两个相等的实根为:x1 = x2 = "+x1); } else if(b * b - 4 * a * c > 0) { temp = Math.sqrt(b * b - 4 * a * c); x1 = (-b+temp)/(2 * a); x2 = (-b-temp)/(2 * a); System.out.println("这个一元二次多项式有两个不相等的实根为:x1 = "+x1+",x2 = "+x2); } else { double real, imaginary; temp = Math.sqrt(4 * a * c - b * b); real = -b/(2 * a); imaginary=temp/(2 * a); System.out.println("这个一元二次多项式有两个虚根为:x1 = "+real+" + "+imaginary+"i"+", x2 = "+real+" - "+imaginary+"i"); } } } public static void main(String[] args) { // TODO Auto-generated method stub Root(1, 2, 1); Root(1, -1, -2); Root(0, 0, 0); Root(0, 0, 6); Root(1, 1, 9); } }

你可能感兴趣的:(求解一元二次多项式的根)