2015-09-12 87 views
0

我確信我缺少某些愚蠢的東西。我想將消息 打印到控制檯窗口,並在同一行顯示最大數組值。將最大數組值寫入控制檯窗口

當我運行的代碼沒有控制檯消息它運行完美,但是當我運行 與消息的代碼,它只顯示消息,沒有最大值。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Arrays 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] newArray = new int[6]; 

      newArray[0] = 0; 
      newArray[1] = 1; 
      newArray[2] = 2; 
      newArray[3] = 49; 
      newArray[4] = 3; 
      newArray[5] = 82; 

      Console.Write("The highest number in the array is: ", newArray.Max()); 
      Console.ReadLine(); 
     } 
    } 
} 

我剛剛開始獲得數組的掛起,但我找不到上述問題的解決方案。

+3

Console.Write(以下簡稱「數組中最大的數字是:{0}「,newArray.Max()); – JOSEFtw

回答

4

一種方法是將字符串:

Console.Write("The highest number in the array is: " + newArray.Max()); 

另一種方式是通過一個複合格式字符串和參數:

Console.Write("The highest number in the array is: {0} ", newArray.Max()); 

最後,如果你的Visual Studio 2015年,你可以做字符串插值:

Console.WriteLine($"The highest number in the array is:{newArray.Max()}") 
1

您還可以使用新的C#6.0的功能,稱爲string interpolation

Console.Write($"The highest number in the array is: {newArray.Max()}");