我有这个任务要求我编写一个代码来确定二次方程的根(ax^2 + bx + c = 0)。但我必须使用大学的图书馆(type.lib.Equation;)。
我几乎把一切都弄清楚了,除了有两个根的情况。我可以得到第一个根,但我仍在四处寻找第二个根
到目前为止我的代码
import java.util.Scanner;
import java.io.PrintStream;
import type.lib.Equation;
public class Check05A
{
/**
* @param args
*/
public static void main(String[] args)
{
PrintStream output = System.out;
Scanner input = new Scanner(System.in);
output.println("Enter a,b,c pressing ENTER after each... ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
output.print("The equation: ");
Equation x = new Equation(a, b, c);
output.print(x);
int root = x.getRootCount();
if(root == 0)
{
output.println(" has no real roots.");
}
if(root == 1)
{
double r1 = x.getRoot(root);
output.println(" has the single root: " + r1);
}
if(root == 2)
{
double r1 = x.getRoot(root);
double r2 = -x.getRoot(root);
output.println(" has the two roots: " + r2 + " and " + r1);
}
if(root == -1)
{
output.println("\nis an identity - any value is a root.");
}
}
}
例如 1, 2, -4 应输出为:
“有两个根:-3.23606797749979 和 1.2360679774997898”