# java 一元二次方程_如何用java编写一元二次方程的求根问题

public class SquareEquation {

double a, b, c;

public void setA(double a) {

this.a = a;

}

public void setB(double b) {

this.b = b;

}

public void setC(double c) {

this.c = c;

}

public double getRootOne() {

double disk = calculateValidDisk();

return (-b + Math.sqrt(disk)) / (2 * a);

}

public double getRootTwo() {

double disk = calculateValidDisk();

return (-b - Math.sqrt(disk)) / (2 * a);

}

private double calculateValidDisk() {

if (a == 0) {

throw new EquationException("不是二次方程", EquationException.NONE_EQUATION);

}

double disk = b * b - 4 * a * c;

if (disk < 0) {

throw new EquationException("没有实根", EquationException.NO_REALROOT);

}

return disk;

}

}

public class Test

{

public static void main(String []args)

{

double a=1;//二次项系数62616964757a686964616fe4b893e5b19e31333366306531

double b=3;//一次项系数

double c=2;//常数项

double s=b*b-4*a*c;

if(s<0)

{

System.out.print("b*b-4ac<0,方程无解!");

return;

}

double ss=Math.sqrt(s);

double x1=(-1*b-ss)/(2*a);

double x2=(-1*b+ss)/(2*a);

System.out.println("x1="+x1);

System.out.println("x2="+x2);

}

}

12-04

03-14
09-08 8085
09-15 7788
05-21 2万+
10-20
03-05 1889
05-16 7106
03-05 100
08-05 2314
03-21 1189
04-06 704
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客