java求解二元一次方程
时间: 2023-04-02 11:01:56 浏览: 53
可以使用以下代码求解二元一次方程:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入二元一次方程的系数:");
System.out.print("a=");
double a = scanner.nextDouble();
System.out.print("b=");
double b = scanner.nextDouble();
System.out.print("c=");
double c = scanner.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=" + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("方程有两个实数解,x1=" + x1 + ",x2=" + x2);
}
}
}
```
阅读全文