Java 程序求二次方程的所有根
javaobject oriented programmingprogramming更新于 2024/4/12 12:28:17
在本文中,我们将了解如何用 Java 计算二次方程的根。二次方程是二阶代数表达式,换句话说,它有两个结果,即实数和虚数。
下面是相同的演示 −
给定一个形式为 ax2 + bx + c 的二次方程 −
有三种情况: b2 < 4*a*c - 根不是实数,即它们是复数 b2 = 4*a*c - 根是实数,并且两个根相同。 b2 > 4*a*c - 根是实数,且两个根不同
输入
假设我们的输入是 −
a = 1, b = 2, c = 3
输出
期望输出将是 −
二次方程的根是 root_1 = -1.00+1.41i root_2 = -1.00-1.41i
算法
步骤 1- 开始 步骤 2- 声明 6 个双精度值:a、b、c、root_1、root_2、quadratic_equation 步骤 3- 提示用户输入 a、b、c 双精度值/定义双精度值 步骤 4- 读取值 步骤 5- 在 for 循环中,检查 quadratic_equation 变量的值是否大于 0,如果 为真,则使用二次公式查找该值,并将其分配给变量。 步骤 6- 显示结果 步骤 7- 停止
示例 1
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);
}
}
}
输出
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
示例 2
这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。
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);
}
}
}
输出
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
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

