2017-01-24 11 views
-1

自分自身で教えるC# 練習には "コーディングの課題"がありましたが、要件の1つを理解するのが難しいです。コンソールアプリケーション - 文字列内の特定のテキストの色を変更する

これはC#Console Applicationで絶対に行う必要があります。私はすでに研究を行っているため、そこに投げ捨てるだけです。私が見つけた答えのほとんどは「これを代わりに使用する」ためです。

課題は、それはあなたが入力名詞、動詞にユーザーに尋ねるMablibsテキスト版の活動ですが、達成することは非常に簡単ですなど

私がこれまで行ってきた2つの文字列の配列を作成してきた何

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

もちろん、ユーザー入力を表示するために、すべてのアクティビティをタイプアウトしました。

しかし、私は変化を強調しなければならない、それはどちらかと言う:

変更下線 - 私はこれはコンソールアプリケーションでは不可能であることを見続けました。

すべての首都 - 簡単なルートですが、私は別のものを学びたいと思います。

大胆な変化 - 私はのStringBuilderに走ったと< B> </b>のほとんどが、このために、私自身でそれを試してみましたが、それは仕事を得ることができませんでした。

異なる色 - 私はConsole.ForegroundColor = ConsoleColor.Magentaを使用することができますが、私はユーザーの入力の色を変更したいと思っています。私は "それを行う"ために多くの方法を見たが、私が試したたびにすべてを変えるだろう。

誰かが何か助けを与えることができたら、私は本当にそれを感謝します。

ありがとうございました。

EDIT:

私はその後のは、ユーザーが入力したとしましょう

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 

最終出力: はたくさん食べるのが好きです。それはスイム曇り探して水が好きです。

ご理解いただけるよう願っております。

+0

色付きのアンサーラインのWriteLineの後に 'Console.ForegroundColor = ConsoleColor.White;'を試したことがありますか? – JohnG

+0

ForegroundColorプロパティを変更できるように、6つの別個のConsole.Write()呼び出しが必要です。簡単なピーシー。 –

答えて

0

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

注:

は、ここに1つの方法です。フォーマットが期待どおりでない場合、それは爆発する。また、最終出力に中括弧を印刷する場合は、追加のコードを記述する必要があります。

+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.WriteLine()の代わりにConsole.Writeを使用してください...名詞、動詞および形容詞を使用する方法を作成してください。例。

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(); 
} 
関連する問題