控制台应用程序 - 更改字符串数组中的特定文本颜色
Console Application - changing specific text color inside a string array
自学c#
我找到了一个 "coding challenge" 练习,但我很难理解如何满足其中一项要求。
它绝对必须在 C# 控制台应用程序中完成 - 只是把它扔在那里,因为我已经完成了我的研究并且我找到的大部分答案都是 "use this instead."
这个挑战很容易完成,它是 Mablibs 文本版本 activity 要求用户输入名词、动词等
到目前为止,我所做的是创建两个字符串数组,其中一个包含要询问用户的不同类型的单词:
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();
}
当然,我将整个 activity 打出来,然后显示用户的输入。
但是,我必须强调这些变化,它说:
在更改下划线 - 我一直看到这在控制台应用程序中是不可能的。
所有首都 - 这将是简单的路线,但我想学习一些不同的东西。
大胆的更改 - 我运行 进入 StringBuilder 和 < b > 主要是为了这个,我自己尝试过,但无法让它工作。
A Different Color - 我知道我可以使用 Console.ForegroundColor = ConsoleColor.Magenta;,但我只想改变用户的颜色输入。我看到了很多 "do it," 的方法,但每次尝试都会改变一切。
如果有人能提供一些帮助,我将不胜感激。
谢谢。
编辑:
我正在努力实现的一个例子
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
最终输出:
一只鸟喜欢吃很多东西。它喜欢在云望水游泳。
希望对您有所帮助。
我不清楚你到底想完成什么,但我只能使用以下方法为一些文本着色:
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("White on blue,");
Console.ResetColor();
Console.Write("but this isn't.");
如果这仍然没有帮助,您需要 post 一个更完整的代码示例,更好地描述您要完成的确切目标。
使用 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();
}
正如 Hans 所建议的,您必须更改前景色 属性 大约 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++;
}
}
请注意,这是非常基本的代码。如果格式不符合预期,它就会爆炸。如果你想在最终输出中打印大括号,你将不得不添加额外的代码。
自学c# 我找到了一个 "coding challenge" 练习,但我很难理解如何满足其中一项要求。
它绝对必须在 C# 控制台应用程序中完成 - 只是把它扔在那里,因为我已经完成了我的研究并且我找到的大部分答案都是 "use this instead."
这个挑战很容易完成,它是 Mablibs 文本版本 activity 要求用户输入名词、动词等
到目前为止,我所做的是创建两个字符串数组,其中一个包含要询问用户的不同类型的单词:
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();
}
当然,我将整个 activity 打出来,然后显示用户的输入。
但是,我必须强调这些变化,它说:
在更改下划线 - 我一直看到这在控制台应用程序中是不可能的。
所有首都 - 这将是简单的路线,但我想学习一些不同的东西。
大胆的更改 - 我运行 进入 StringBuilder 和 < b > 主要是为了这个,我自己尝试过,但无法让它工作。
A Different Color - 我知道我可以使用 Console.ForegroundColor = ConsoleColor.Magenta;,但我只想改变用户的颜色输入。我看到了很多 "do it," 的方法,但每次尝试都会改变一切。
如果有人能提供一些帮助,我将不胜感激。
谢谢。
编辑:
我正在努力实现的一个例子
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
最终输出: 一只鸟喜欢吃很多东西。它喜欢在云望水游泳。
希望对您有所帮助。
我不清楚你到底想完成什么,但我只能使用以下方法为一些文本着色:
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("White on blue,");
Console.ResetColor();
Console.Write("but this isn't.");
如果这仍然没有帮助,您需要 post 一个更完整的代码示例,更好地描述您要完成的确切目标。
使用 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();
}
正如 Hans 所建议的,您必须更改前景色 属性 大约 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++;
}
}
请注意,这是非常基本的代码。如果格式不符合预期,它就会爆炸。如果你想在最终输出中打印大括号,你将不得不添加额外的代码。