用JAVA写一个求解一元二次方程的类
展开全部
import java.util.Scanner;
public class equation {
public static void main(String[] args){
System.out.println("输入一元二次系数a,b,c:");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
double x1,x2;
int deta=b*b-4*a*c;
if(deta==0){
x1=x2=(-b)/(2*a);
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}else{
double t=Math.sqrt(Math.abs(deta))/(2*a);
if(deta>0){
x1=(-b)/(2*a)+t;
x2=(-b)/(2*a)-t;
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}else{
x1=(-b)/(2*a);
x2=(-b)/(2*a);
System.out.println("x1="+x1+"+"+t+"*i");
System.out.println("x2="+x2+"-"+t+"*i");
}
}
}
}
public class equation {
public static void main(String[] args){
System.out.println("输入一元二次系数a,b,c:");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
double x1,x2;
int deta=b*b-4*a*c;
if(deta==0){
x1=x2=(-b)/(2*a);
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}else{
double t=Math.sqrt(Math.abs(deta))/(2*a);
if(deta>0){
x1=(-b)/(2*a)+t;
x2=(-b)/(2*a)-t;
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}else{
x1=(-b)/(2*a);
x2=(-b)/(2*a);
System.out.println("x1="+x1+"+"+t+"*i");
System.out.println("x2="+x2+"-"+t+"*i");
}
}
}
}
展开全部
public double A(double db){
if(db<0){
double dou=-db;
double res = Math.sqrt(dou);
System.out.println("虚根:"+ -res);
return -res;
}
double res = Math.sqrt(db);
System.out.println("实根:"+ res);
return res;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询