在C#中打印数组时,我将所有值组合在一起

时间:2018-03-26 17:39:55

标签: c#

对于C#中的以下代码,我的数组输出为 " 12152288"而不是它们之间的空格。我该怎么做才能解决这个问题? Here is the Output error I get.这是代码。

static void Main()
{
    int[] numbers = {12, 15, 22, 88};
    int x;
    double average;
    double total = 0;
    Console.Write("\nThe numbers are...");
    for(x = 0; x < numbers.Length; ++x)
        Write("{0}", numbers[x]);
    Console.WriteLine();      
    for(x = 0; x < numbers.Length; ++x)
    {
        total = total + numbers[x];
    }   
    average = total / numbers.Length;
    Console.Write("The average is {0}", average);
}

1 个答案:

答案 0 :(得分:2)

更改此行:

Write("{0}", numbers[x]);

对此:

Console.Write("{0} ", numbers[x]);