用java写一个计算一元二次方程的代码
时间: 2023-03-23 13:04:25 浏览: 107
可以使用以下代码计算一元二次方程:
import java.util.Scanner;
public class QuadraticEquation {
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 < ) {
System.out.println("方程无实数解");
} else if (delta == ) {
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);
}
}
}
阅读全文