IF阻止到LOOP

时间:2016-01-02 11:13:31

标签: c#

我真的需要缩短我的代码并将大量IF操作符块转换为LOOP(for,foreach)。

当它在IF块中时 - 代码有效。 当它在FOR循环中时 - 代码有时不起作用。

帮助会很好。

IF阻止:

            PictureBox tmp = new PictureBox();
        tmp.Bounds = pbxDators.Bounds;

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 3;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 2;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 1;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = 0;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -1;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -2;
            return true;
        }

        tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);

        if (dt.Bounds.IntersectsWith(tmp.Bounds))
        {
            atskanotAudio(1);
            bumbasStiprums = -3;
            return true;
        }

        return false;

我尝试用FOR循环来替换那些大量的IF:

            for (int i = -3; i <= 3; i++)
        {
            PictureBox tmp = new PictureBox();
            tmp.Bounds = pbxDators.Bounds;
            int a = 10;

            if (i == 3)
            {
                tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
                if (dt.Bounds.IntersectsWith(tmp.Bounds))
                {
                    atskanotAudio(2);
                    bumbasStiprums = 2;
                    return true;
                }
            }
            else
            {
                tmp.SetBounds(tmp.Location.X, tmp.Location.Y + a, 1, 15);
                a = a + 10;
                if (dt.Bounds.IntersectsWith(tmp.Bounds))
                {
                    atskanotAudio(2);
                    bumbasStiprums = 2;
                    return true;
                }
            }
        }

        return false;

所有代码都在一个公共函数中,它将picturebox对象作为参数并返回bool值..

为什么我的FOR循环在大多数情况下不起作用?如果球击中中间,它有时可以,但大多数球飞过物体界限。没有错误产生。

1 个答案:

答案 0 :(得分:2)

根据您的第一个代码,这似乎是您需要的代码:

PictureBox tmp = new PictureBox();
tmp.Bounds = pbxDators.Bounds;

tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);

for (var i = 3; i >= -3; i--)
{
    if (dt.Bounds.IntersectsWith(tmp.Bounds))
    {
        atskanotAudio(1);
        bumbasStiprums = i;
        return true;
    }

    tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
}

return false;