Java编写程序,从键盘输入一元二次方程的系数a、b、
时间: 2023-05-27 14:07:02 浏览: 95
c,求解并输出方程的根。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一元二次方程的系数a:");
double a = input.nextDouble();
System.out.print("请输入一元二次方程的系数b:");
double b = input.nextDouble();
System.out.print("请输入一元二次方程的系数c:");
double c = input.nextDouble();
double delta = b * b - 4 * a * c;
if (delta < 0) {
System.out.println("方程无实数根");
} else if (delta == 0) {
double x = -b / (2 * a);
System.out.println("方程的唯一实数根为:" + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("方程的两个实数根分别为:" + x1 + "和" + x2);
}
}
}
```
运行截图:
![](https://cdn.jsdelivr.net/gh/Febers/images/2021/05/20210522163600.png)
阅读全文