2016-11-29 6 views
1

私は星から矩形を作成してテキストの中に入れようとしていますが、わかりません。誰でも助けてくれますか?矩形内の単語C#

string s = Console.ReadLine(); 
string[] n = s.Split(' '); 
int longest = 0; 
for (int i = 0; i < n.Length; i++) 
{ 
    if(n[i].Length > longest) 
    { 
     longest = n[i].Length; 
    } 
} 
for (int i = 1; i <= n.Length + 2; i++) 
{ 
    for (int j = 1; j <= longest + 2; j++) 
    { 
     if (i == 1 || i == n.Length + 2 || j == 1 || j == longest + 2) 
     { 
      Console.Write("*");      
     } 
     if (i == 2 && j == 1) 
     { 
      Console.Write(n[0]); 
     } 
     else 
      Console.Write(""); 
    } 
    Console.WriteLine(); 
} 
Console.ReadLine(); 

私は1つの単語を置くことができますが、それでも問題はありませんが、配列インデックスの数を変更すると正しく動作しません。

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

+3

あるHello My World!入力の場合

// please, think about variables' names: what is "n", "s"? // longest is NOT the longest word, but maxLength string text = Console.ReadLine(); // be nice: allow double spaces, take tabulation into account string[] words = text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); // Linq is often terse and readable int maxLength = words.Max(word => word.Length); // try keep it simple (try avoiding complex coding) // In fact, we have to create (and debug) top string top = new string('*', maxLength + 2); // ... and body: // for each word in words we should // - ensure it has length of maxLength - word.PadRight(maxLength) // - add *s at both ends: "*" + ... + "*" string body = string.Join(Environment.NewLine, words .Select(word => "*" + word.PadRight(maxLength) + "*")); // and, finally, join top, body and top string result = string.Join(Environment.NewLine, top, body, top); // final output Console.Write(result); 

あなたは、所望の出力の*例*を提供していただけますか?例えば。 「こんにちは私の世界!」の入力に? –

+1

うわー、どのような複雑なソフトウェア。私はそれが何をしているのだろうか –

+1

こんにちはt.rとスタックオーバーフローへようこそ。希望の出力に加えて、発生している問題についても具体的に説明してください。箱は全く表示されていませんか?それはあなたがそれを期待どおりにフォーマットされていませんか?これは問題の絞り込みに役立ちます。 –

答えて

3

マルチワードバージョン出力が

******** 
*Hello * 
*My * 
*World!* 
********