📜  求二次方程根的Java程序

📅  最后修改于: 2022-05-13 01:55:02.659000             🧑  作者: Mango

求二次方程根的Java程序

函数的根是 x 截距。根据定义,位于 x 轴上的点的 y 坐标为零。因此,为了找到二次函数的根,我们设置 f (x) = 0,并求解方程 ax 2 + bx + c = 0。

二次方程的条件 -

ax^2 + bx + c = 0 

where 
a, b, c are real numbers and cannot be zero ie, there value must be from {-∞ to -1} and {1 to ∞}

求二次方程根的数学公式 -

roots = (-b ± √(b2-4ac)) / (2a)

± represents there are two roots.

二次方程的根是——

first = (-b + √(b2-4ac)) / (2a)
second = (-b - √(b2-4ac)) / (2a)

作为行列式的 (b^2 – 4ac) 告诉我们根的性质 –

  1. 如果(b^2 – 4ac) > 0 ,则根是实数且不同
  2. 如果(b^2 – 4ac) == 0 ,根是实数且相等
  3. 如果(b^2 – 4ac) < 0 ,根复杂且不同

求二次方程根的代码:

Java
// Java program to find the roots of
// quadratic equation
  
public class Main {
  
    public static void main(String[] args)
    {
  
        // value of the constants a, b, c
        double a = 7.2, b = 5, c = 9;
  
        // declared the two roots
        double firstroot, secondroot;
  
        // determinant (b^2 - 4ac)
        double det = b * b - 4 * a * c;
  
        // check if determinant is greater than 0
        if (det > 0) {
  
            // two real and distinct roots
            firstroot = (-b + Math.sqrt(det)) / (2 * a);
            secondroot = (-b - Math.sqrt(det)) / (2 * a);
  
            System.out.format(
                "First Root = %.2f and Second Root = %.2f",
                firstroot, secondroot);
        }
  
        // check if determinant is equal to 0
        else if (det == 0) {
  
            // two real and equal roots
            // determinant is equal to 0
            // so -b + 0 == -b
            firstroot = secondroot = -b / (2 * a);
  
            System.out.format(
                "First Root = Second Root = %.2f;",
                firstroot);
        }
  
        // if determinant is less than zero
        else {
  
            // roots are complex number and distinct
            double real = -b / (2 * a);
  
            double imaginary = Math.sqrt(-det) / (2 * a);
  
            System.out.printf("First Root = %.2f+%.2fi",
                              real, imaginary);
            System.out.printf("\nSecond Root = %.2f-%.2fi",
                              real, imaginary);
        }
    }
}


输出
First Root = -0.35+1.06i
Second Root = -0.35-1.06i