我正在编写基于二次方程式的程序,对我来说一切都很好,并且从我看到的内容来看没有语法或逻辑错误,并且Eclipse在运行之前未检测到任何错误。 这是代码的输出: 线程“主”中的异常java.lang.StackOverflowError 在QuadraticEquation.getDiscriminant(QuadraticEquation.java:33) 请注意,它会像这样持续约50行左右
project properties-Configuration Properties-NMake-Additional Options: -lrt -ldl
public class QuadraticEquation {
private double a;
private double b;
private double c;
public QuadraticEquation() {
double a = 0;
double b = 0;
double c = 0;
}
public QuadraticEquation(double newA, double newB, double newC) {
a = newA;
b = newB;
c = newC;
}
public double discriminant1 = Math.pow(b, 2) - 4 * a * c;
public double discriminant2 = Math.pow(b, 2) - 4 * a * c;
public double getA() {
return getA();
}
public double getB() {
return getB();
}
public double getC() {
return getC();
}
public double getDiscriminant() {
double discriminant = (b * 2) - (4 * a * c);
return getDiscriminant();
}
public double getRoot1() {
double r1 = (-1*b) + Math.sqrt(discriminant1) / (2*a);
return getRoot1();
}
public double getRoot2() {
double r2 = (-1*b) - Math.sqrt(discriminant2) / (2*a);
return getRoot2();
}
public void setA(double newA1) {
a = newA1;
}
public void setB(double newB1) {
b = newB1;
}
public void setC(double newC1) {
c = newC1;
}
}
答案 0 :(得分:1)
您的错误在这里是无限递归的:
public double getDiscriminant() {
double discriminant = (b * 2) - (4 * a * c);
return getDiscriminant();
}
此函数将无限调用自身,直到堆栈溢出为止。我相信您想改为返回变量discriminant
?
与您的功能getRoot1
,getRoot2
,getA
,getB
和getC
相同。