为什么不使用c#在控制台上显示变量的输出

时间:2013-05-07 12:51:51

标签: c#

/**
Write a program in C# language to perform the following operation:

b. Finding greatest of n numbers

**/
using System;
using System.Linq;


class Greatest
{
    public static void Main(String[] args)
    {   
        //get the number of elements
        Console.WriteLine("Enter the number of elements");
        int n;
        n=Convert.ToInt32(Console.ReadLine());
        int[] array1 = new int[n];         
        //accept the elements
        for(int i=0; i<n; i++)
        {
            Console.WriteLine("Enter element no:",i+1);
            array1[i]=Convert.ToInt32(Console.ReadLine());
        }
        //Greatest
        int max=array1.Max();
        Console.WriteLine("The max is ", max);

        Console.Read();
    }
}

程序不输出变量的值,我无法弄清楚为什么??

示例输出

 Enter the number of elements
3
Enter element no:
2
Enter element no:
3
Enter element no:
2
The max is 

注意变量没有输出。

由于

3 个答案:

答案 0 :(得分:13)

您错过了format item,例如

更改...

Console.WriteLine("The max is ", max);

为...

Console.WriteLine("The max is {0}", max);

答案 1 :(得分:4)

Console.WriteLine("The max is " + max);

会奏效。

答案 2 :(得分:1)

你可能也想要这个:

Console.WriteLine("Enter element no. {0}:", i+1);