需要帮助在C#中创建2D数组

时间:2016-04-20 21:14:53

标签: c# arrays multidimensional-array

我在C#中创建了一个2维数组,但我无法将我的行和列排成一行。我试图将我的数据分为六行和五列。周一与12,10,17,22排队。然后周二与11,12,17,22排队。这将继续坐下来。这是表格的一个例子。

Example of the Table view in the console

这是我到目前为止构建的代码。

class Zumba
{
    public static void main()
    {
        Zumba table = new Zumba();
        int[,] zumValues = table.ZumbaValues;
        string[] zumForm = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
        for (int z = 0; z < zumForm.GetLength(0); z++)
        {
            Console.Write("{0}", zumForm[z]);
            for (int r = 0; r < zumValues.GetLength(0); r++)
            {
                for (int c = 0; c < zumValues.GetLength(1); c++)
                    Console.Write("\t" + "{1,2,3,4,5,6}" + "\t", zumValues[r, c]);

            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }

    private int[,] zumba = new int[6, 4] { { 12, 10, 17, 22 }, 
                                           { 11, 13, 17, 22 }, 
                                           { 12, 10, 22, 22 }, 
                                           { 9, 14, 17, 22 }, 
                                           { 12, 10, 21, 12 }, 
                                           { 12, 10, 5, 10 } };

    public int[,] ZumbaValues
    {
        get
        {
            return zumba;
        }
        set
        {
            zumba = value;
        }
    }
}

4 个答案:

答案 0 :(得分:0)

您有一个2-dimensional array,但还有3个for-loop。此外,"{1,2,3,4,5,6}"不是有效的字符串格式。

public static void Main()
{
    Zumba table = new Zumba();
    int[,] zumValues = table.ZumbaValues;
    string[] zumForm = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

    for (int day = 0; day < zumForm.Length; day++)
    {
        Console.Write(zumForm[day]);

        for (int time = 0; time < zumValues.GetLength(1); time++)
            Console.Write("\t{0}", zumValues[day, time]);

        Console.WriteLine();
    }
}

答案 1 :(得分:0)

这应该有用;

Program table = new Program();
            int[,] zumValues = table.ZumbaValues;
            string[] zumForm = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
            for (int z = 0; z < zumForm.Length; z++)
            {
                var Nums = "";
                for (var t = 0; t < 4; t++)
                    Nums = Nums + table.zumba[z, t].ToString() + " ";

                Console.Write("{0} {1}", zumForm[z],Nums);
                Console.WriteLine();
            }

            Console.ReadLine();

答案 2 :(得分:0)

我看到你有3个循环而你只需要2个,一个用于行,一个用于cols。此外,您不需要{1,2,3,4,5,6},因为您正在骑自行车而且您只需要一个参数{0}

这是我认为你需要的:

static public void Main()
{
    Zumba table = new Zumba();
    int[,] zumValues = table.ZumbaValues;
    string[] zumForm = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
    for (int z = 0; z < zumForm.GetLength(0); z++)
    {
        Console.Write("{0}", zumForm[z]);
        for (int c = 0; c < 4; c++)
             Console.Write("\t"+"{0}"+"\t",zumValues[z, c]);
        Console.Write("\n");
    }

    Console.ReadLine();

}

答案 3 :(得分:0)

这是一个有效的简单解决方案:

public static void Main()
    {
        Zumba table = new Zumba();
        int[,] zumValues = table.ZumbaValues;
        string[] zumForm = new string[6] { "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };


        int r = 0;
        while (r < zumValues.GetLength(0))
        {
            //Write the day
            Console.Write("{0}", zumForm[r]);
            //right the zumba values
            for (int c = 0; c < zumValues.GetLength(1); c++)
                Console.Write("\t" + zumValues[r, c] + "\t");   
            //new Line             
            Console.Write("\n");
            r++;
        }

        Console.ReadLine();
    }