java编写二元一次方程测试代码
时间: 2023-04-09 14:01:35 浏览: 68
我可以回答这个问题。以下是一个简单的Java代码,用于测试二元一次方程:
```
import java.util.Scanner;
public class EquationTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a value for a: ");
double a = input.nextDouble();
System.out.print("Enter a value for b: ");
double b = input.nextDouble();
System.out.print("Enter a value for 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.");
}
}
}
```
这个程序会要求用户输入二元一次方程的系数,然后计算并输出方程的根。