-1

我怎样才能使下面的代码更高效,更少的行。

我将一些 PictureBox 元素添加到二维数组中。

        int a = 0;
        int b = 0;
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Iteration: " + i + " a = " + a);
            Console.WriteLine("Iteration: " + i + " b = " + b);
            pictureBoxArr[a, b] = new PictureBox();
            b++;
        }

        int aa = 1;
        int bb = 0;
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Iteration: " + i + " aa = " + aa);
            Console.WriteLine("Iteration: " + i + " bb = " + bb);
            pictureBoxArr[aa, bb] = new PictureBox();
            bb++;
        }

        int aaa = 2;
        int bbb = 0;

        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Iteration: " + i + " aaa = " + aaa);
            Console.WriteLine("Iteration: " + i + " bbb = " + bbb);
            pictureBoxArr[aaa, bbb] = new PictureBox();
            bbb++;
        }

我在想这样的事情-但我有点卡住了。

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; i++)
            {

            }
        }
4

2 回答 2

0

尝试:

for (int i = 0; i < 3; i++)
{
 for (int j = 0; j < 3; j++)
   {
        Console.WriteLine("Iteration: " + i + " a = " + i);
        Console.WriteLine("Iteration: " + j + " b = " + j);
        pictureBoxArr[i, j] = new PictureBox();
   }
}

哥们,你很亲近

于 2013-10-08T20:09:14.790 回答
0
 for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            pictureBoxArr[i, j] = new PictureBox();
        }
    }

你几乎拥有它。

如果你想要一些更可重用的东西,你可以设置几个变量来保持你的界限。

    int boundX = 10;
    int boundY = 10;
    for (int i = 0; i < boundX ; i++)
    {
        for (int j = 0; j < boundY ; j++)
        {
            pictureBoxArr[i, j] = new PictureBox();
        }
    }
于 2013-10-08T20:04:05.813 回答