怎么样用Java编一元二次方程求解,其中要加入复数解.
怎么样用Java编一元二次方程求解,其中要加入复数解.
import java.util.*;
class First
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一元二次方程的三个系数a,b,c的值");
System.out.println("请输入系数a的值");
double a = sc.nextDouble();
System.out.println("请输入系数b的值");
double b = sc.nextDouble();
System.out.println("请输入系数c的值");
double c = sc.nextDouble();
double deta = b*b-4*a*c;
if(deta>=0)
{
System.out.println("该方程有实数解!");
double x1 = (-b+Math.sqrt(deta))/(2*a);
double x2 = (-b-Math.sqrt(deta))/(2*a);
System.out.println("x1="+x1+",x2="+x2);
}else
{
System.out.println("该方程有虚数解!");
double shibu = -b/(2*a);
double xubu = Math.sqrt(-deta)/(2*a);
System.out.println("x1="+shibu+"+"+xubu+"i");
System.out.println("x2="+shibu+"-"+xubu+"i");
}
}
}