用Java求解一元二次方程的代码
时间: 2024-06-12 18:10:01 浏览: 83
以下是用Java求解一元二次方程的代码:
```java
import java.util.Scanner;
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
double a = input.nextDouble();
System.out.print("Enter b: ");
double b = input.nextDouble();
System.out.print("Enter c: ");
double c = input.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.println("The roots are " + root1 + " and " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("The root is " + root);
} else {
System.out.println("The equation has no real roots.");
}
}
}
```
阅读全文