1

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

1 回答 1

1

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

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);
    …
  }
}

如果您需要管理不同的返回类型,则稍微复杂一些:只需添加一些与返回值/类型对应的 out 参数即可。

于 2015-07-10T08:38:50.763 回答