2013-08-25 43 views
2

我在學習MSDN tutorial的C#數組語法。它有這樣的代碼:MSDN C#數組教程

// Array-of-arrays (jagged array) 
byte[][] scores = new byte[5][]; 

// Create the jagged array 
for (int i = 0; i < scores.Length; i++) 
{ 
    scores[i] = new byte[i + 3]; 
} 

// Print length of each row 
for (int i = 0; i < scores.Length; i++) 
{ 
    Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length); 
    Console.ReadLine(); 
} 

,並表示輸出爲:

Length of row 0 is 3 
Length of row 1 is 4 
Length of row 2 is 5 
Length of row 3 is 6 
Length of row 4 is 7 

我有複製粘貼代碼到一個控制檯應用程序的主要方法和我的控制檯輸出爲:

Length of row 0 is 3 

任何人都知道我爲什麼輸出不同?

+1

按下以下行 – anderZubi

+0

感謝所有,很多答案的正確輸入。 – user2602079

回答

5

您的程序要求你打輸出的連續行之間的輸入:

Console.ReadLine(); 
3
  1. 你在那裏Console.ReadLine(),所以你必須按下回車鍵它顯示輸出的下一行之前。
  2. Visual Studio(如果您使用它)調試器是您的朋友(或一般調試器)。
2
 byte[][] scores = new byte[5][]; 

     // Create the jagged array 
     for (int i = 0; i < scores.Length; i++) 
     { 
      scores[i] = new byte[i + 3]; 
     } 

     // Print length of each row 
     for (int i = 0; i < scores.Length; i++) 
     { 
      Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length); 
      Console.ReadLine(); <------ Press enter here to continue, If you want your output like MSDN's, remove this line and the program will output all results 
     } 
+0

對不起,修改 – User2012384