2016-04-20 100 views
0

我在C#中创建了一个2维数组,我无法让我的行和列排成一列。我正在试图获取六行五列的数据。 Mon与12,10,17,22排队。然后周二与11,12,17,22排队。这将继续坐着。这是表格的一个例子。需要帮助在C#中创建一个二维数组#

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; 
     } 
    } 
} 
+0

那么问题是什么?控制台的图像看起来很好。 – Frecklefoot

+0

对于其中一个,这条线看起来错了'Console.Write(“\ t”+“{1,2,3,4,5,6}”+“\ t”,zumValues [r,c]);' – Icemanind

回答

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(); 
    } 
} 
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(); 
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(); 

} 
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(); 
    }