亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單行 C# 控制臺中的多色字符

單行 C# 控制臺中的多色字符

C#
慕村9548890 2022-07-23 18:09:41
如何僅使用基本命令(例如和)在一行中打印多色字符?例如我需要類似的東西: Console.WriteConsole.WriteLineConsole.WriteLine($"red{M}green{U}blue{L}yellow{T}purple{I}");其中M和是不同的值,U例如or 。Lintstring
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

這是我會使用的,可能不是最好的方法,但它確實有效。


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

        }

        

    }


查看完整回答
反對 回復 2022-07-23
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

您可以使用如下代碼所示的字典,然后使用循環:


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)

        {

            ConsoleColor color = keyValuePairs[keyItem].Value;

            string textTobeDisplayed = keyValuePairs[keyItem].Key;


            Console.ForegroundColor = color;

            Console.Write(textTobeDisplayed);

        }


查看完整回答
反對 回復 2022-07-23
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

我希望這對你有幫助


class Program

{

    static void Main(string[] args)

    {

        Console.ForegroundColor = ConsoleColor.Blue;

        Console.Write("White on Blue.");

        Console.ForegroundColor = ConsoleColor.Red;

        Console.Write("White on Red.");

        Console.ResetColor();

        Console.ReadLine();

    }

}


查看完整回答
反對 回復 2022-07-23
  • 3 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號