java解一元二次方程
时间: 2023-10-29 16:47:32 浏览: 51
Sure, I can help you with that! Here's a simple Java program that can solve a quadratic equation:
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) {
System.out.println("No real solutions.");
} else {
double x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("x1 = " + x1 + ", x2 = " + x2);
}
}
}
You can enter the values of a, b, and c when prompted, and the program will output the solutions (if any). Hope that helps!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)