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

Convert two mutually recursive methods into single recursive method in java?

我需要获取一个包含两个相互递归方法的程序并修改该程序,使其包含一个递归方法。据我了解,我需要通过将递归调用按照调用顺序放在一个方法中来组合这两种递归方法。问题是有 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);
    …
  }
}

如果需要管理不同的return类型就稍微复杂一点:只需要添加一些对应return values/types.

的out参数即可