「Java案例」求解一元二次方程

168 阅读6分钟

案例解析

一元二次方程求解

怎样求一元二次方程 的在实数域上的解(即实根)?判断式△=b²-4ac,先判断△,若△<0,则原方程无实根;若△=0,则原方程有两个相同的解,为x=-b/2a;若△>0,则x=(-b±根号下△)/2a。 编写程序,提示输入a、b和c的值,并输出结果。如果△为正,打印2个根;如果△为0,打印1个根;否则,输出“No real roots”。

# 源文件保存为“QuadraticEquationSolver.java”
import java.util.Scanner;

public class QuadraticEquationSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("求解一元二次方程 ax² + bx + c = 0");
        System.out.print("请输入系数a:");
        double a = scanner.nextDouble();
        
        System.out.print("请输入系数b:");
        double b = scanner.nextDouble();
        
        System.out.print("请输入系数c:");
        double c = scanner.nextDouble();
        
        // 计算判别式
        double discriminant = b * b - 4 * a * c;
        
        if (discriminant > 0) {
            // 两个不同实根
            double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.printf("方程有两个实根:x1 = %.2f, x2 = %.2f%n", root1, root2);
        } else if (discriminant == 0) {
            // 一个实根(重根)
            double root = -b / (2 * a);
            System.out.printf("方程有一个实根:x = %.2f%n", root);
        } else {
            // 无实根
            System.out.println("方程无实根");
        }
        
        scanner.close();
    }
}

运行结果: 依次输入1、-5、6

求解一元二次方程 ax² + bx + c = 0
请输入系数a:1
请输入系数b:-5
请输入系数c:6
方程有两个实根:x1 = 3.00, x2 = 2.00

代码解析:

  • 输入处理:使用Scanner类获取用户输入的三个系数a、b、c
  • 判别式计算:计算△=b²-4ac,这是判断根情况的关键
  • 三种情况处理
    • △>0:计算两个不同的实根
    • △=0:计算唯一实根(重根)
    • △<0:输出无实根提示
  • 输出结果:使用printf格式化输出,保留两位小数

复数根求解

# 源文件保存为“ComplexRootSolver.java”
import java.util.Scanner;

public class ComplexRootSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("求解一元二次方程 ax² + bx + c = 0");
        System.out.print("请输入系数a:");
        double a = scanner.nextDouble();

        System.out.print("请输入系数b:");
        double b = scanner.nextDouble();

        System.out.print("请输入系数c:");
        double c = scanner.nextDouble();

        double discriminant = b * b - 4 * a * c;

        if (discriminant > 0) {
            // 两个不同实根
            double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.printf("方程有两个实根:x1 = %.2f, x2 = %.2f%n", root1, root2);
        } else if (discriminant == 0) {
            // 一个实根(重根)
            double root = -b / (2 * a);
            System.out.printf("方程有一个实根:x = %.2f%n", root);
            // 实根处理与基础案例相同
        } else {
            // 计算复数根
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            System.out.printf("方程有两个复数根:x1 = %.2f + %.2fi, x2 = %.2f - %.2fi%n",
                    realPart, imaginaryPart, realPart, imaginaryPart);
        }

        scanner.close();
    }
}

运行结果 依次输入101

求解一元二次方程 ax² + bx + c = 0
请输入系数a:1
请输入系数b:0
请输入系数c:1
方程有两个复数根:x1 = -0.00 + 1.00i, x2 = -0.00 - 1.00i

代码特点:

  • 处理复数根情况
  • 计算实部和虚部
  • 输出复数形式的根
  • 扩展了基础案例的功能

系数验证和异常处理

import java.util.InputMismatchException;
import java.util.Scanner;

public class RobustQuadraticSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("请输入系数a(不能为0):");
            double a = scanner.nextDouble();
            if (a == 0) {
                System.out.println("a不能为0,这不是二次方程!");
                return;
            }

            System.out.print("请输入系数b:");
            double b = scanner.nextDouble();

            System.out.print("请输入系数c:");
            double c = scanner.nextDouble();

            double discriminant = b * b - 4 * a * c;

            if (discriminant > 0) {
                // 两个不同实根
                double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
                double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
                System.out.printf("方程有两个实根:x1 = %.2f, x2 = %.2f%n", root1, root2);
            } else if (discriminant == 0) {
                // 一个实根(重根)
                double root = -b / (2 * a);
                System.out.printf("方程有一个实根:x = %.2f%n", root);
                // 实根处理与基础案例相同
            } else {
                // 计算复数根
                double realPart = -b / (2 * a);
                double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
                System.out.printf("方程有两个复数根:x1 = %.2f + %.2fi, x2 = %.2f - %.2fi%n",
                        realPart, imaginaryPart, realPart, imaginaryPart);
            }
        } catch (InputMismatchException e) {
            System.out.println("输入错误,请输入数字!");
        } finally {
            scanner.close();
        }
    }
}

运行结果 输入0

请输入系数a(不能为0):0
a不能为0,这不是二次方程!

代码亮点:

  • 验证a不能为0
  • 处理非数字输入异常
  • 更健壮的错误处理
  • 保证资源释放

操作练习题

添加顶点坐标计算

要求:

  • 计算并输出抛物线的顶点坐标
  • 顶点公式:(-b/2a, c-b²/4a)

参考代码:

# 源文件保存为“QuadraticEquationSolver.java”
import java.util.Scanner;

public class QuadraticEquationSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("求解一元二次方程 ax² + bx + c = 0");
        System.out.print("请输入系数a:");
        double a = scanner.nextDouble();

        System.out.print("请输入系数b:");
        double b = scanner.nextDouble();

        System.out.print("请输入系数c:");
        double c = scanner.nextDouble();

        double vertexX = -b / (2 * a);
        double vertexY = c - (b * b) / (4 * a);
        System.out.printf("抛物线顶点坐标:(%.2f, %.2f)%n", vertexX, vertexY);

        scanner.close();
    }
}

运行结果 依次输入121

求解一元二次方程 ax² + bx + c = 0
请输入系数a1
请输入系数b2
请输入系数c:1
抛物线顶点坐标:(-1.00, 0.00)

方程图像绘制

要求:

  • 使用星号(*)简单绘制抛物线图像
  • 在控制台输出x从-5到5的图像

参考代码:

# 源文件保存为“QuadraticEquationSolver.java”
import java.util.Scanner;

public class QuadraticEquationSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("求解一元二次方程 ax² + bx + c = 0");
        System.out.print("请输入系数a:");
        double a = scanner.nextDouble();

        System.out.print("请输入系数b:");
        double b = scanner.nextDouble();

        System.out.print("请输入系数c:");
        double c = scanner.nextDouble();

        System.out.println("\n抛物线图像预览:");
        for (int x = -5; x <= 5; x++) {
            double y = a * x * x + b * x + c;
            int stars = (int) Math.round(y);
            System.out.printf("x=%2d: ", x);
            for (int i = 0; i < stars; i++) {
                System.out.print("*");
            }
            System.out.println();
        }

        scanner.close();
    }
}

运行结果 依次输入121

求解一元二次方程 ax² + bx + c = 0
请输入系数a:1
请输入系数b:2
请输入系数c:1

抛物线图像预览:
x=-5: ****************
x=-4: *********
x=-3: ****
x=-2: *
x=-1: 
x= 0: *
x= 1: ****
x= 2: *********
x= 3: ****************
x= 4: *************************
x= 5: ************************************

多方程求解

要求:

  • 允许用户连续求解多个方程
  • 输入0作为a值退出程序

参考代码:

# 源文件保存为“QuadraticEquationSolver.java”
import java.util.Scanner;

public class QuadraticEquationSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("求解一元二次方程 ax² + bx + c = 0");

            System.out.print("\n请输入系数a(输入0退出):");

            System.out.print("请输入系数a:");
            double a = scanner.nextDouble();
            if (a == 0) break;

            System.out.print("请输入系数b:");
            double b = scanner.nextDouble();

            System.out.print("请输入系数c:");
            double c = scanner.nextDouble();

            double discriminant = b * b - 4 * a * c;

            if (discriminant > 0) {
                // 两个不同实根
                double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
                double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
                System.out.printf("方程有两个实根:x1 = %.2f, x2 = %.2f%n", root1, root2);
            } else if (discriminant == 0) {
                // 一个实根(重根)
                double root = -b / (2 * a);
                System.out.printf("方程有一个实根:x = %.2f%n", root);
                // 实根处理与基础案例相同
            } else {
                // 计算复数根
                double realPart = -b / (2 * a);
                double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
                System.out.printf("方程有两个复数根:x1 = %.2f + %.2fi, x2 = %.2f - %.2fi%n",
                        realPart, imaginaryPart, realPart, imaginaryPart);
            }
            System.out.println("----------------------");
        }
        scanner.close();
    }
}

运行结果 依次输入数据

求解一元二次方程 ax² + bx + c = 0

请输入系数a(输入0退出):请输入系数a:1
请输入系数b:2
请输入系数c:1
方程有一个实根:x = -1.00
----------------------
求解一元二次方程 ax² + bx + c = 0

请输入系数a(输入0退出):请输入系数a:0