2017-02-21 35 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