2016-04-20 98 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(); 
    }