如何减少本例中的代码重复

发布于 2024-09-08 18:06:38 字数 695 浏览 11 评论 0原文

我需要循环一个数字(xx)。 xx 始终从零开始。我的问题是,如果 moveDirection 变量为 +1,则 xx 会增加,直到达到 range 的正值。如果 moveDirection 为 -1,则 xx 减小,直到达到 range 的负值。

在下面的代码中,我首先通过 if 语句测试 moveDirection 来完成此操作,然后复制 for 循环,并编辑每种情况的值。我的代码恰好是在 ActionScript3 中,但语言并不重要。

var p:Point;
var xx:int;

if (moveDirection > 0)
{
    for (xx = 0; xx < range; xx++)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}
else 
{
    for (xx = 0; xx > range; xx--)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}

有没有更好的方法来做到这一点,也许不需要重复 for 循环?如果有任何其他建议,将不胜感激。

I need to loop through a number (xx). xx always starts at zero. My problem is that if the moveDirection variable is +1 then xx increases until it reaches the positive of range. If moveDirection is -1, then xx decreases until reaching the negative of range.

In the code below, I have done this by having an if statement test for moveDirection first, then I have duplicated the for loop, and edited the values for each case. My code happens to be in ActionScript3, but the language does not matter.

var p:Point;
var xx:int;

if (moveDirection > 0)
{
    for (xx = 0; xx < range; xx++)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}
else 
{
    for (xx = 0; xx > range; xx--)
    {
        if (hitTestPoint(xx, yy))
        {
            return true;
        }
    }
}

Is there a better way of doing this, maybe without duplicating the for loop? If there is any other advice, it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷遇一生 2024-09-15 18:06:38
for (xx = 0; xx != range; xx += moveDirection)
{
    if (hitTestPoint(xx, yy))
    {
        return true;
    }
}

假设 moveDirection 分别为 1 或 -1(向上或向下)。另外,您必须稍微更改范围才能使 != 正常工作。但是,它确实减少了代码。

for (xx = 0; xx != range; xx += moveDirection)
{
    if (hitTestPoint(xx, yy))
    {
        return true;
    }
}

This assumes that moveDirection will be either 1 or -1 for going up or down, respectively. Also, you'll have to slightly change your range for the != to work properly. But, it does cut down on code.

安人多梦 2024-09-15 18:06:38

从代码的外观来看,循环运行的方向并不重要——如果 hitTestPoint 返回 true,您只是返回 true > 对于范围内的某个值。如果是这样,另一种可能性是这样的:

var start:int = min(0, range);
var stop:int = max(0, range);

for (xx = start; xx!=stop; xx++)
    if (hitTestPoint(xx,yy)
        return true;

From the looks of the code, it doesn't really matter which direction the loop runs -- you're just returning true if hitTestPoint returns true for some value in the range. If that's so, another possibility would be something like:

var start:int = min(0, range);
var stop:int = max(0, range);

for (xx = start; xx!=stop; xx++)
    if (hitTestPoint(xx,yy)
        return true;
无声情话 2024-09-15 18:06:38

另一种可能性:

int i;
for (i = abs(range), xx = 0; --i >= 0; xx += moveDirection){
  if (hitTestPoint(xx, yy) return true;
}

Another possibility:

int i;
for (i = abs(range), xx = 0; --i >= 0; xx += moveDirection){
  if (hitTestPoint(xx, yy) return true;
}
坠似风落 2024-09-15 18:06:38

这是 Java 中的一个示例(另请参阅 ideone.com):

static void go(final int range, final int direction) {
    for (int i = 0; i != direction*range; i += direction) {
        System.out.println(i);
    }       
}

然后您可以执行以下操作:

        go(5, +1); // 0, 1, 2, 3, 4
        go(5, -1); // 0, -1, -2, -3, -4

如果您想适应非单位步骤,最简单的定义第三个参数如下:

static void go(final int range, final int step, final int direction) {
    for (int i = 0; i < range; i += step) {
        System.out.println(i * direction);
    }       
}

然后你可以这样做:

        go(10, 3, +1); // 0, 3, 6, 9
        go(10, 3, -1); // 0, -3, -6, -9

Here's an example in Java (see also on ideone.com):

static void go(final int range, final int direction) {
    for (int i = 0; i != direction*range; i += direction) {
        System.out.println(i);
    }       
}

Then you can do:

        go(5, +1); // 0, 1, 2, 3, 4
        go(5, -1); // 0, -1, -2, -3, -4

If you want to accommodate non-unit step, it's simplest to define a third parameter as follows:

static void go(final int range, final int step, final int direction) {
    for (int i = 0; i < range; i += step) {
        System.out.println(i * direction);
    }       
}

Then you can do:

        go(10, 3, +1); // 0, 3, 6, 9
        go(10, 3, -1); // 0, -3, -6, -9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文