自學自我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();
}
當然我輸入了整個活動,然後顯示用戶輸入。
,但我要強調的變化和它說要麼:
強調變化 - 我不斷看到,這是不可能的控制檯應用程序。
All Capitals - 這將是簡單的路線,但我想學習不同的東西。
大膽的變化 - 我跑進的StringBuilder和< B> </b>的多爲這並試圖對我自己,但無法得到它的工作。
不同的顏色 - 我知道我可以使用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();
}
然後讓我們假設用戶輸入一個例子:鳥,游泳,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
最終輸出: A 小鳥喜歡吃很多。它喜歡游泳在雲水看水。
我希望能幫助你理解。
您是否嘗試過'Console.ForegroundColor = ConsoleColor.White;'在彩色答案行的WriteLine之後? – JohnG
您只需要6個獨立的Console.Write()調用,以便您可以更改ForegroundColor屬性。十分簡單。 –