Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java tìm tất cả nghiệm của phương trình bậc hai


Trong bài viết này, chúng ta sẽ tìm hiểu cách tính nghiệm của phương trình bậc hai trong Java. Phương trình bậc hai là biểu thức đại số bậc hai, có hai nghiệm: thực và ảo.

Cho phương trình bậc hai dạng ax² + bx + c:

Có ba trường hợp:
b² < 4ac - Nghiệm không thực (phức)
b² = 4ac - Nghiệm thực và bằng nhau
b² > 4ac - Nghiệm thực và khác nhau

Đầu vào

Giả sử đầu vào:

a = 1, b = 2, c = 3

Đầu ra

Kết quả mong đợi:

Nghiệm của phương trình bậc hai là root_1 = -1.00+1.41i, root_2 = -1.00-1.41i

Thuật toán

Bước 1 - BẮT ĐẦU
Bước 2 - Khai báo 6 biến double: a, b, c, root_1, root_2, quadratic_equation
Bước 3 - Nhập a, b, c từ người dùng hoặc định nghĩa trước
Bước 4 - Đọc giá trị
Bước 5 - Tính delta = b*b - 4*a*c. Nếu delta > 0: tính hai nghiệm thực. Nếu delta == 0: nghiệm kép. Nếu delta < 0: tính phần thực và ảo.
Bước 6 - Hiển thị kết quả
Bước 7 - DỪNG

Ví dụ 1: Nhập từ bàn phím

Người dùng nhập a, b, c dựa trên lời nhắc. Thử trực tiếp trong coding ground.

import java.util.Scanner;
public class QuadraticEquation {
    public static void main(String[] args) {
        double a, b, c, root_1, root_2, quadratic_equation;
        double real_number, imaginary_number;
        System.out.println("Required packages have been imported");
        Scanner my_scanner = new Scanner(System.in);
        System.out.println("A scanner object has been defined");
        System.out.print("Enter the value of a: ");
        a = my_scanner.nextDouble();
        System.out.print("Enter the value of b: ");
        b = my_scanner.nextDouble();
        System.out.print("Enter the value of c: ");
        c = my_scanner.nextDouble();
        quadratic_equation = b * b - 4 * a * c;
        if (quadratic_equation > 0) {
            root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
            root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
            System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
        } else if (quadratic_equation == 0) {
            root_1 = root_2 = -b / (2 * a);
            System.out.format("root_1 = root_2 = %.2f;", root_1);
        } else {
            real_number = -b / (2 * a);
            imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
            System.out.println("The roots of the quadratic equation are");
            System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
            System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
        }
    }
}

Đầu ra

Required packages have been imported
A scanner object has been defined
Enter the value of a: 1
Enter the value of b: 2
Enter the value of c: 3
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

Ví dụ 2: Giá trị định nghĩa trước

Ba số a, b, c đã được định nghĩa trước.

public class QuadraticEquation {
    public static void main(String[] args) {
        double a, b, c, root_1, root_2, quadratic_equation;
        double real_number, imaginary_number;
        a = 1;
        b = 2;
        c = 3;
        System.out.println("The three numbers are defined as " + a + ", " + b + " and " + c);
        quadratic_equation = b * b - 4 * a * c;
        if (quadratic_equation > 0) {
            root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
            root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
            System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
        } else if (quadratic_equation == 0) {
            root_1 = root_2 = -b / (2 * a);
            System.out.format("root_1 = root_2 = %.2f;", root_1);
        } else {
            real_number = -b / (2 * a);
            imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
            System.out.println("The roots of the quadratic equation are");
            System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
            System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
        }
    }
}

Đầu ra

The three numbers are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i