gpt4 book ai didi

java - 在java中将两个相互递归方法转换为单个递归方法?

转载 作者:行者123 更新时间:2023-11-30 08:04:31 25 4
gpt4 key购买 nike

我需要采用一个具有两个相互递归方法的程序并修改该程序,使其包含一个递归方法。据我了解,我需要通过将递归调用按照调用顺序放置在单个方法中来组合这两个递归方法。问题是有 4 个整数通过方法传递,第一个方法调用第二个方法两次,第二个方法调用第一个方法两次。

这是原始代码:

public void drawHorizontal(Graphics graphics, double xMid, double yMid, double length )
{
// find left endpoint
double x1 = xMid - (length / 2);
double y1 = yMid;

// find right endpoint
double x2 = xMid + (length / 2);
double y2 = yMid;

if (length > 5)
{
// draw a line from (x1,y1) to (x2,y2)
graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

// draw a vertical line with left end of horizontal as midpoint of new line
drawVertical(graphics, x1, y1, (length) );

// draw a vertical line with right endof horizontal as midpoint of new line
drawVertical(graphics, x2, y2, (length) );
}

} // end drawHorizontal()

public void drawVertical(Graphics graphics, double xMid, double yMid, double length )
{
// find upper endpoint
double x1 = xMid;
double y1 = yMid - (length / 2);

// right lower endpoint
double x2 = xMid;
double y2 = yMid + (length / 2);


if (length > 5)
{
// draw a line from (x1,y1) to (x2,y2)
graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

// draw a 1/2 size horizontal line with top end of vertical as midpoint of new line
drawHorizontal(graphics, x1, y1, (length/2) );

// draw a 1/2 horizontal line with bottom end of vertical as midpoint of new line
drawHorizontal(graphics, x2, y2, (length/2) );
}

} // end drawVertical()

下面是我最近修改的代码。我知道它很难看,但我就是不知道如何独立地调整 x 和 y 坐标。我试图通过创建更多变量来解决这个问题,但我忍不住觉得我只是做了更多的工作。我能找到的最接近的堆栈问题是 this 。我从 11 点就开始了,现在已经是 4 点 15 分了。如果您能够向正确的方向插入,我们将不胜感激,感谢您的宝贵时间。

编辑*感谢您的快速回复,我很感激。我知道以这种方式分解相互递归方法似乎违反直觉,但我对编程和 java 很陌生,所以我正在探索分解问题的不同方法。这就是我最终将其分解为的内容,并且运行良好。感谢您抽出时间。

修改后的代码:

    public void Total(Graphics graphics, boolean type, double xMid, double yMid, double length) {

double x1;
double y1;

// find right endpoint
double x2;
double y2;

if (type == false) {
// find left endpoint
x1 = xMid - (length / 2);
y1 = yMid;

// find right endpoint
x2 = xMid + (length / 2);
y2 = yMid;

if (length > 5) {
// draw a line from (x1,y1) to (x2,y2)
graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

// draw a vertical line with left end of horizontal as midpoint of new line
Total(graphics, true, x1, y1, (length));

// draw a vertical line with right endof horizontal as midpoint of new line
Total(graphics, true, x2, y2, (length));
}
} else {

// find upper endpoint
x1 = xMid;
y1 = yMid - (length / 2);

// right lower endpoint
x2 = xMid;
y2 = yMid + (length / 2);

if (length > 5) {
// draw a line from (x1,y1) to (x2,y2)
graphics.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

// draw a 1/2 size horizontal line with top end of vertical as midpoint of new line
Total(graphics, false, x1, y1, (length / 2));

// draw a 1/2 horizontal line with bottom end of vertical as midpoint of new line
Total(graphics, false, x2, y2, (length / 2));
}
}
}

最佳答案

你的要求很奇怪,相互递归通常有助于阅读......但是好吧,(至少)有一种通用方法。如果您有:

f(f_params) {

g(g_params…);

}
g(g_params) {

f(f_params…);

}

可以这样转换:

fg(what,f_params,g_params) {
if (what==FUNC_F) {

fg(FUNC_G,f_params,g_params);

} else if (what==FUNC_G) {

fg(FUNG_F,f_params,g_params);

}
}

如果需要管理不同的返回类型,那就有点复杂了:只需添加一些与返回值/类型相对应的输出参数即可。

关于java - 在java中将两个相互递归方法转换为单个递归方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com