40 lines
1.1 KiB
Java
40 lines
1.1 KiB
Java
import java.util.Scanner;
|
|
import java.lang.Math;
|
|
class EquationRoot
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
Scanner scan = new Scanner(System.in);
|
|
double a,b,c,disc,x1,x2,p,q;
|
|
|
|
System.out.print("请输入系数a=");
|
|
a = scan.nextDouble() ;
|
|
|
|
System.out.print("请输入系数b=");
|
|
b = scan.nextDouble() ;
|
|
|
|
System.out.print("请输入系数c=");
|
|
c = scan.nextDouble() ;
|
|
|
|
disc=b*b-4*a*c;
|
|
|
|
if ( Math.abs(disc) <= 1e-6 )
|
|
System.out.println("x1=x2="+(-b/(2*a)));
|
|
else
|
|
{
|
|
if(disc>1e-6)
|
|
{
|
|
x1=(-b+Math.sqrt(disc))/(2*a);
|
|
x2=(-b-Math.sqrt(disc))/(2*a);
|
|
System.out.printf("x1=%7.2f,x2=%7.2f\n", x1, x2);
|
|
}
|
|
else
|
|
{
|
|
p=-b/(2*a);
|
|
q=Math.sqrt(Math.abs(disc))/(2*a);
|
|
System.out.printf("x1=%7.2f + %7.2f i\n", p, q);
|
|
System.out.printf("x2=%7.2f - %7.2f i\n", p, q);
|
|
}
|
|
}
|
|
}
|
|
} |