2

我正在从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

任何人都知道为什么我的输出不同?

4

3 回答 3

5

您的程序要求您在连续的输出行之间按 Enter 键:

Console.ReadLine();
于 2013-08-25T03:26:55.227 回答
3
  1. 你有一个Console.ReadLine()在那里,所以你必须在它显示下一行输出之前按回车。
  2. Visual Studio(如果您正在使用它)调试器是您的朋友(或一般的调试器)。
于 2013-08-25T03:28:36.940 回答
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
        }
于 2013-08-25T03:29:07.877 回答