因此,getArea调用getHeight,后者调用getArea并创建一个永无止境的循环。
我在编写尽可能少的代码并重用尽可能多的函数方面得到了评分。
无论如何,我可以通过函数调用来完成此工作,而不是仅将论坛的getArea替换为getHeight,其中调用getArea吗?
//returns the area of a trapezoid
public double getArea(){
double height = getHeight();
double area = (.5 * ((getPoint(3).getX() - getPoint(4).getX()) + (getPoint(2).getX() - getPoint(1).getX()))) * height;
return area;
}
//returns the height of a trapezoid
public double getHeight(){
double area = getArea();
double height = (2 * area) / (((getPoint(3).getX() - getPoint(4).getX()) + (getPoint(2).getX() - getPoint(1).getX())));
return height;
}
最佳答案
供审查:
void a() {
b();
}
void b() {
a();
}
bool binarySearch(int* list, int searchFor, int size) {
if (size == 1 && list[0] != searchFor)
return false;
int split = size / 2;
if (list[split] == searchFor) {
return true;
} else if (list[split] > searchFor) {
binarySearch(list, searchFor, split);
} else {
binarySearch(list[split], searchFor, split);
}
void a() {
a();
}
在“答案”上:
因此,我需要更多信息才能为您提供最简单的答案,但这至少是一个选择。依赖于您拥有x和y坐标(其他任何人都可以随意添加)。
使用点来查找高度:
您似乎可以调用.x()来获取x值,因此,如果存在y值,只需通过取每个点对(A&D,D&C,C&B和B&A)的差值来找到高度。
对于每一个,您将有一个 vector
(m.x, m.y) = (a.x, a.y) - (b.x, b.y)
,您可以找到magnitude = sqrt(m.x * m.x + m.y * m.y)
的大小。现在您拥有了所有四个边长,您可以像这样计算高度(a&b是顶部和底部,c&d是边):
关于java - 如何避免此递归函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898737/