0
我有這個任務,要求我編寫一個代碼來確定二次方程(ax^2 + bx + c = 0)的根。但我必須使用大學的圖書館(type.lib.Equation;)。使用特定類找出二次方程的根
我幾乎弄清楚了一切,除了有兩個根的情況。我可以得到1根,但我仍然盤旋,以獲得第二根
到目前爲止我的代碼
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」
我將如何獲得第二個值? 我知道如果我手動設置getRoot()爲1,2(1表示兩個根中的較小者,2較大),我會得到兩個根。 – Smartian