2017-01-24 21 views
-1

自學自我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 小鳥喜歡吃很多。它喜歡游泳雲水看水。

我希望能幫助你理解。

+0

您是否嘗試過'Console.ForegroundColor = ConsoleColor.White;'在彩色答案行的WriteLine之後? – JohnG

+0

您只需要6個獨立的Console.Write()調用,以便您可以更改ForegroundColor屬性。十分簡單。 –

回答

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++; 
     } 
    } 

注意,這是非常基本的代碼。如果格式不符合預期,它就會爆炸。如果要在最終輸出中打印大括號,則必須添加其他代碼。

+0

我按照你的指導原則修改了它,並且它可以工作,非常感謝! – Pandda

0

這是我不太清楚你要完成什麼,但我能使用下面的上色只有一些文字:

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

如果仍然沒有幫助,你需要發佈更完整的代碼示例,更好地描述您正在嘗試完成的內容。

+0

添加了一個我正在尋找的例子 – Pandda

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(); 
} 
相關問題