2013-05-07 61 views
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(); 
    } 
} 

該程序不輸出變量的值,我不明白爲什麼?爲什麼變量的輸出顯示在控制檯上使用c#

示例輸出是

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

注意沒有從變量輸出。

感謝

+1

因爲你不告訴它!請參閱George的回答 – Yahya 2013-05-07 12:54:03

回答

13

你缺少你format item,例如

更改...

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

到...

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

或'Console.WriteLine(「最大值是」+最大值);' – James 2013-05-07 12:54:27

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

會工作。

1

你也可以這樣:

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