Java程序来查找二次方程的根
二次方程的根由以下公式确定:
$$x=\frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$
计算根-
计算行列式值(b*b)-(4*a*c)。
如果行列式大于0,则根为[-b+平方根(行列式)]/2*a和[-b-平方根(行列式)]/2*a。
如果行列式等于0,则根值为(-b+Math.sqrt(d))/(2*a)
示例
import java.util.Scanner;
public class RootsOfQuadraticEquation {
public static void main(String args[]){
double secondRoot = 0, firstRoot = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of a ::");
double a = sc.nextDouble();
System.out.println("Enter the value of b ::");
double b = sc.nextDouble();
System.out.println("Enter the value of c ::");
double c = sc.nextDouble();
double determinant = (b*b)-(4*a*c);
double sqrt = Math.sqrt(determinant);
if(determinant>0){
firstRoot = (-b + sqrt)/(2*a);
secondRoot = (-b - sqrt)/(2*a);
System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);
}else if(determinant == 0){
System.out.println("Root is :: "+(-b + sqrt)/(2*a));
}
}
}输出结果
Enter the value of a :: 15 Enter the value of b :: 68 Enter the value of c :: 3 Roots are :: -0.044555558333472335 and -4.488777774999861
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练