c# - C# 控制台应用程序中的 Macron

标签 c# console codepages ascii-art

我正在尝试使用斜杠和破折号创建一个堡垒,并且我需要使用宏(overscore)。我尝试了几种方法,但没有一个有效。有什么想法如何让它发挥作用吗?我得到的不是马克龙,而是“?”

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);
       }
    }
}

最佳答案

控制台可以使用不同的代码页来显示文本。并非所有这些代码页都可以正确显示所有字符。例如,当您使用代码页 855(这是使用带有西里尔字母的东欧语言的 Windows 系统的默认值)时,无法显示 Macron 字符。

您可以通过在 Main 函数开头设置 Console.OutputEncoding 来定义用于自己的控制台输出的代码页:

Console.OutputEncoding = Encoding.Unicode;

这将使控制台可以显示任何 Unicode 字符。

如果您想使用特定的代码页,例如 437(美国),您可以编写:

Console.OutputEncoding = Encoding.GetEncoding(437);

请注意,在此位置使用 Encoding.Unicode 至少需要 .net 版本 4.5。

参见documentation of property Console.OutputEncoding了解更多详情。

关于c# - C# 控制台应用程序中的 Macron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42373097/

相关文章:

c# - 选项卡上的监听器单击或激活无法正常工作/extjs4

javascript - console.log() 不输出 jQuery 选择对象的 HTML

c - 使用 MinGW GCC 在 Windows CMD 上更改代码页时,将 _POSIX_C_SOURCE 定义为 2 会导致错误

powershell - GHCi:尽管字体和代码页正确,但文本输出不正确

xml - 如何在 Visual Studio 2013 中制作我自己的 "resx"文件版本?

c# - 限制 .NET 类型是否只能通过 ByVal 或 ByRef

c# - 如何反序列化自定义对象列表

ruby-on-rails - 如何通过 capistrano 进入生产环境的 Rails 控制台?

javascript - 使 Firebug 控制台历史记录在浏览器退出时保留

VB.NET 如何强制 Asc 在非英语系统上使用英语代码页