猿问

单行 C# 控制台中的多色字符

如何仅使用基本命令(例如和)在一行中打印多色字符?例如我需要类似的东西: Console.WriteConsole.WriteLine

Console.WriteLine($"red{M}green{U}blue{L}yellow{T}purple{I}");

其中M和是不同的值,U例如or 。Lintstring


慕村9548890
浏览 146回答 3
3回答

繁星淼淼

这是我会使用的,可能不是最好的方法,但它确实有效。class Program{    static void Main(string[] args)    {        // ~ red, ` green, ^ blue, * yellow, _ purple        ColorWrite("~M`u^l*t_i", true);        ColorWrite("~This is red,^ yet this is blue.", true);        ColorWrite("~Mul`ti^ple ", false);        ColorWrite("*Col_ours ", false);    }    // variable contains both consoleColor and char    struct ColourKey    {        public ConsoleColor color;        public char key;        public ColourKey(ConsoleColor Color, char Key)        {            this.color = Color;            this.key = Key;        }    }    static void ColorWrite(string rawtext, bool endline)    {        //all avaliable colours, for more just make the array bigger        ColourKey[] Pallete = new ColourKey[5];        Pallete[0] = new ColourKey(ConsoleColor.Red, '~');        Pallete[1] = new ColourKey(ConsoleColor.Green, '`');        Pallete[2] = new ColourKey(ConsoleColor.Blue, '^');        Pallete[3] = new ColourKey(ConsoleColor.Yellow, '*');        Pallete[4] = new ColourKey(ConsoleColor.DarkMagenta, '_'); //ConsoleColor does not contain purple        foreach (char c in rawtext)        {            bool CanWrite = true;            foreach (ColourKey ck in Pallete)            {                if (c == ck.key)                {                    Console.ForegroundColor = ck.color;                    CanWrite = false;                }            }            if (CanWrite)            {                Console.Write(c);            }        }        Console.ResetColor();        // true function works like writeline(), false works like write()        if (endline)        {            Console.WriteLine();        }            }

富国沪深

您可以使用如下代码所示的字典,然后使用循环:Dictionary<int, KeyValuePair<string, ConsoleColor>> keyValuePairs = new Dictionary<int, KeyValuePair<string, ConsoleColor>>();keyValuePairs.Add(1, new KeyValuePair<string, ConsoleColor>("my blue text", ConsoleColor.Blue));keyValuePairs.Add(2, new KeyValuePair<string, ConsoleColor>("my red text", ConsoleColor.Red));foreach (var keyItem in keyValuePairs.Keys)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConsoleColor color = keyValuePairs[keyItem].Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string textTobeDisplayed = keyValuePairs[keyItem].Key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(textTobeDisplayed);&nbsp; &nbsp; &nbsp; &nbsp; }

红颜莎娜

我希望这对你有帮助class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Blue;&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("White on Blue.");&nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Red;&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("White on Red.");&nbsp; &nbsp; &nbsp; &nbsp; Console.ResetColor();&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答