如何在C#中使用while循环创建多维数组

时间:2022-11-29 20:46:33

I'm a beginner in c# and I have to write a multidimensional array using while loop. I just can't go through it. So far I have made what I exactly need with nested for loop but can't convert it to while loop. Any help would be greatly appreciated.

我是c#的初学者,我必须使用while循环编写一个多维数组。我只是无法通过它。到目前为止,我已经使用嵌套for循环制作了我真正需要的东西,但无法将其转换为while循环。任何帮助将不胜感激。

int[][] tab = new int[4][];
tab[0] = new int[4];
tab[1] = new int[3];
tab[2] = new int[2];
tab[3] = new int[1];

int num = 1;

for(int i = 0; i < tab.Length; i++)
{
    for(int j = 0; j < tab[i].Length; j++)
    {
        tab[i][j] = num++;
    }
}

for(int i = 0; i < tab.Length; i++)
{
    Console.Write("tab[{0}] = ", i);
    for(int j = 0; j < tab[i].Length; j++)
    {
        Console.Write("{0} ", tab[i][j]);
    }
    Console.WriteLine(""); 
}

3 个解决方案

#1


0  

Updated answer to remove redundant console write line loop

更新了删除冗余控制台写行循环的答案

while (ai < tab.Length)
{
    bj = 0;
    Console.Write("tab[{0}] = ", ai);
    while (bj < tab[ai].Length)
    {
        tab[ai][bj] = num++;
        Console.Write("{0} ", tab[ai][bj]);
        bj++;
    }
    Console.WriteLine("");
    ai++;
}

#2


3  

A for loop is a dressed up while loop, you're already very close.

for循环是一个打扮的循环,你已经非常接近了。

consider that

考虑一下

for(a; b; c) { ...; }

is equal to (without the break/continue labels for brevity)

等于(没有中断/继续标签以简洁)

 { a; while (b) { ...; c; } }

You should be able to do this conversion easily. But I think a teacher should also just accept code with a for loop as it is functionally a while loop.

您应该可以轻松地进行此转换。但我认为老师也应该接受带有for循环的代码,因为它在功能上是一个while循环。

Now there could be other constraints in your homework, like allowing nested loops or the [,] or [][] style multi dimensional arrays (again, functionally the same, different C# notations for slightly different feature sets).

现在你的作业中可能还有其他限制,例如允许嵌套循环或[,]或[] []样式的多维数组(同样,功能相同,不同的C#符号用于稍微不同的功能集)。

#3


0  

           int[][] tab = new int[4][];
            tab[0] = new int[4];
            tab[1] = new int[3];
            tab[2] = new int[2];
            tab[3] = new int[1];

            int num = 1;

            int ai = 0;
            int bj = 0;

            while (ai < tab.Length) {
                bj = 0;
                while (bj < tab[ai].Length) {
                    tab[ai][bj] = num++;
                    bj++;
                }
                ai++;
            }

            for (int i = 0; i < tab.Length; i++)
            {
                Console.Write("tab[{0}] = ", i);
                for (int j = 0; j < tab[i].Length; j++)
                {
                    Console.Write("{0} ", tab[i][j]);
                }
                Console.WriteLine("");
            }

Just remove for loop and added while loop.

只需删除for循环并添加while循环。

#1


0  

Updated answer to remove redundant console write line loop

更新了删除冗余控制台写行循环的答案

while (ai < tab.Length)
{
    bj = 0;
    Console.Write("tab[{0}] = ", ai);
    while (bj < tab[ai].Length)
    {
        tab[ai][bj] = num++;
        Console.Write("{0} ", tab[ai][bj]);
        bj++;
    }
    Console.WriteLine("");
    ai++;
}

#2


3  

A for loop is a dressed up while loop, you're already very close.

for循环是一个打扮的循环,你已经非常接近了。

consider that

考虑一下

for(a; b; c) { ...; }

is equal to (without the break/continue labels for brevity)

等于(没有中断/继续标签以简洁)

 { a; while (b) { ...; c; } }

You should be able to do this conversion easily. But I think a teacher should also just accept code with a for loop as it is functionally a while loop.

您应该可以轻松地进行此转换。但我认为老师也应该接受带有for循环的代码,因为它在功能上是一个while循环。

Now there could be other constraints in your homework, like allowing nested loops or the [,] or [][] style multi dimensional arrays (again, functionally the same, different C# notations for slightly different feature sets).

现在你的作业中可能还有其他限制,例如允许嵌套循环或[,]或[] []样式的多维数组(同样,功能相同,不同的C#符号用于稍微不同的功能集)。

#3


0  

           int[][] tab = new int[4][];
            tab[0] = new int[4];
            tab[1] = new int[3];
            tab[2] = new int[2];
            tab[3] = new int[1];

            int num = 1;

            int ai = 0;
            int bj = 0;

            while (ai < tab.Length) {
                bj = 0;
                while (bj < tab[ai].Length) {
                    tab[ai][bj] = num++;
                    bj++;
                }
                ai++;
            }

            for (int i = 0; i < tab.Length; i++)
            {
                Console.Write("tab[{0}] = ", i);
                for (int j = 0; j < tab[i].Length; j++)
                {
                    Console.Write("{0} ", tab[i][j]);
                }
                Console.WriteLine("");
            }

Just remove for loop and added while loop.

只需删除for循环并添加while循环。