控制台应用程序 - 更改字符串数组中的特定文本颜色

时间:2017-01-24 04:04:24

标签: c# console-application textcolor

自我教学c# 我发现了练习的“编码挑战”,但我很难理解如何完成其​​中一项要求。

绝对必须在C#控制台应用程序中完成 - 因为我已经完成了我的研究,并且我发现的大部分答案都是“使用它”。

挑战很容易实现,它是一个Mablibs文本版本活动,您要求用户输入名词,动词等。

到目前为止,我所做的一直是创建两个字符串数组,其中一个包含要询问用户的不同类型的单词:

string[] prompt = {"noun","verb","adverb"} //this contains 12 strings

和另一个包含用户输入的数组,因为我将使用for循环来获取它们的输入,类似于:

For (int i = 0; i < userAnswer.Length; i++)
{
Console.Write("Please enter a/an " + prompt[i] + ": ");
userAnswer[i] = Console.ReadLine();
}

当然,我输出了整个活动,然后显示用户输入。

但是,我必须强调这些变化,它要么说:

强调更改 - 我一直看到这在Console App中无法实现。

所有首都 - 这将是简单的路线,但我想学习不同的东西。

大胆改变 - 我遇到了StringBuilder和&lt; b> &LT; / b&gt;主要是为了这个并且我自己尝试了,但是无法让它工作。

不同的颜色 - 我知道我可以使用Console.ForegroundColor = ConsoleColor.Magenta;,但我只想更改用户输入的颜色。我看到很多方法可以“做到”,但每次尝试都会改变一切。

如果有人能提供一些帮助,我真的很感激。

感谢。

修改

我正在努力实现的一个例子

string[] answerHolder = {"","",""}; //MY originaly code has 13, but I am doing 3 to write it out faster
string[] prompt = {"noun", "verb", "adjective"};

Console.Readline("Help me finish the story:");
Console.Readline("A <noun> likes to eat a lot. It likes to <verb> in the <adjective> looking water. ");
//then it will ask the user to enter a noun, verb, and adjective
for(int i = 0; i < answerHolder.Length; i++)
{ 
Console.Write("Please enter a/an " + prompt[i] + ": ");
answerHolder[i] = Console.ReadLine();
}

然后让用户输入:bird,swim,cloudly

//Then I want to display it back but change the color of each
//element that was stored inside answerHolder to emphasize what they entered
Console.Writeline("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder[0], answerHolder[1], answerHolder[2]);
//Code to change color or bold

最终输出: 喜欢吃很多东西。它喜欢在 cloudly 看水中游泳

我希望这有助于你理解。

3 个答案:

答案 0 :(得分:0)

我不清楚你到底想要完成什么,但我只能使用以下内容为一些文字着色:

    Console.BackgroundColor = ConsoleColor.Blue;
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("White on blue,");
    Console.ResetColor();
    Console.Write("but this isn't.");

如果这仍然没有帮助,您需要发布一个更完整的代码示例,更好地描述您要完成的内容。

答案 1 :(得分:0)

使用Console.Write代替Console.WriteLine()来简化事情......制作一个采用名词,动词和形容词的方法。实施例。

PrintAnswer("bird", "swim", "cloudy");

此方法。

private static void PrintAnswer(string noun, string verb, string adjective) {
  Console.Write("A ");
  Console.ForegroundColor = ConsoleColor.Yellow;
  Console.Write(noun);
  Console.ForegroundColor = ConsoleColor.White;
  Console.Write(" likes to eat a lot. It likes to ");
  Console.ForegroundColor = ConsoleColor.Yellow;
  Console.Write(verb);
  Console.ForegroundColor = ConsoleColor.White;
  Console.Write(" in the ");
  Console.ForegroundColor = ConsoleColor.Yellow;
  Console.Write(adjective);
  Console.ForegroundColor = ConsoleColor.White;
  Console.WriteLine(" looking water.");
  Console.ResetColor();
}

答案 2 :(得分:0)

正如汉斯建议的那样,你必须将前景颜色属性改变大约6倍(根据你的最终输出)。你能做的最好的事情就是把这个逻辑放在一个循环中。

以这种方式:

    static void Main(string[] args)
    {
        string[] answerHolder = { "", "", "" }; //MY originaly code has 13, but I am doing 3 to write it out faster
        string[] prompt = { "noun", "verb", "adjective" };

        Console.WriteLine("Help me finish the story:");
        Console.WriteLine("A <noun> likes to eat a lot. It likes to <verb> in the <adjective> looking water. ");
        //then it will ask the user to enter a noun, verb, and adjective
        for (int i = 0; i < answerHolder.Length; i++)
        {
            Console.Write("Please enter a/an " + prompt[i] + ": ");
            answerHolder[i] = Console.ReadLine();
        }

        //Console.WriteLine("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder[0], answerHolder[1], answerHolder[2]);
        WriteFormattedLine("A {0} likes to eat a lot. It likes to {1} in the {2} looking water.", answerHolder);

        Console.ReadLine();
    }

    private static void WriteFormattedLine(string format, params string[] answers)
    {
        int formatLength = format.Length;
        int currIndex = 0;
        bool readingNumber = false;
        string numberRead = string.Empty;
        while (currIndex < formatLength)
        {
            var ch = format[currIndex];
            switch (ch)
            {
                case '{':
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    readingNumber = true;
                    numberRead = string.Empty;
                    break;
                case '}':
                    var number = int.Parse(numberRead);
                    var answer = answers[number];
                    Console.Write(answer);
                    Console.ResetColor();
                    readingNumber = false;
                    break;
                default:
                    if (readingNumber)
                        numberRead += ch;
                    else
                        Console.Write(ch);
                    break;
            }

            currIndex++;
        }
    }

请注意,这是非常基本的代码。如果格式不符合预期,它就会爆炸。如果要在最终输出中打印大括号,则必须添加其他代码。