2017-02-21 36 views
2

我試圖創建一個使用斜線和破折號的堡壘,我需要使用macron(overscore)。我嘗試了幾種方法,但沒有一個能夠工作。任何想法如何使其工作?而不是Macron我得到'?'cron控制檯應用程序中的Macron

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

namespace kingsdom 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int n = int.Parse(Console.ReadLine()); 
      string ups = new string ('^', n/2); 
      string midUps = new string('_' ,(n * 2) - (((n/2) * 2) + 4)); 
      string whitespaces = new string(' ', (n * 2) - 2); 
      string downs = new string('_', n/2); 
      string midDowns = new string('\u203E', (n * 2) - (((n/2) * 2) + 4)); 
      Console.WriteLine("{0}{1}{2}{3}{0}{1}{2}", "/", ups, "\\", midUps); 
      for (int i = 0; i < n - 2; i++) 
      { 
       Console.WriteLine("{0}{1}{0}", "|", whitespaces); 
      } 
      Console.WriteLine("{0}{1}{2}{3}{0}{1}{2}", "\\", downs, "/", midDowns); 
     } 
    } 
} 
+0

究竟是什麼問題?你的代碼似乎對我很好。 – Chris

+0

@Chris而不是Macron我得到「?」 – VG98

+0

這可能意味着您的字體不支持該字符。嘗試https://dotnetfiddle.net/DOyG5w,希望能夠工作(您的瀏覽器可能具有unicode字體)。 – Chris

回答

3

控制檯可以使用不同的代碼頁來顯示文本。並非所有這些代碼頁都可以正確顯示所有字符。例如,當您使用代碼頁855(這是使用西裏爾字母的使用東歐語言的Windows系統的默認設置)時,無法顯示Macron字符。

您可以定義哪些代碼頁是由你的主要功能年初設定Console.OutputEncoding用於自己的控制檯輸出:

Console.OutputEncoding = Encoding.Unicode; 

這將讓這個控制檯可以顯示任何Unicode字符。

如果你想使用一個特定的代碼頁例如像437(美國),你可以這樣寫:

Console.OutputEncoding = Encoding.GetEncoding(437); 

注意,在這個地方使用Encoding.Unicode至少需要.NET版本4.5。

詳情請參閱documentation of property Console.OutputEncoding