2
/**
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 

注意变量没有输出。

谢谢

4

3 回答 3

13

您缺少格式项目,例如

改变...

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

至...

Console.WriteLine("The max is {0}", max);
于 2013-05-07T12:53:15.823 回答
4
Console.WriteLine("The max is " + max);

将工作。

于 2013-05-07T12:54:53.410 回答
1

你可能还想要这个:

Console.WriteLine("Enter element no. {0}:", i+1);
于 2013-05-07T13:05:41.363 回答