C# 矩形c中的单词#
我试图从星星中创建一个矩形,并将其放入文本中,但我想不出来。有人能帮我吗C# 矩形c中的单词#,c#,arrays,console-application,C#,Arrays,Console Application,我试图从星星中创建一个矩形,并将其放入文本中,但我想不出来。有人能帮我吗 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 &l
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();
string s=Console.ReadLine();
字符串[]n=s.Split(“”);
int=0;
对于(int i=0;i最长)
{
最长=n[i]。长度;
}
}
对于(inti=1;i多词版本
// 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);
对于Hello My World!
输入,输出为
********
*Hello *
*My *
*World!*
********
请您提供一个所需输出的示例?例如,在Hello My World!
input上?哇,这是一个多么复杂的软件。我想知道它有什么作用。欢迎使用Stack Overflow。除了所需输出外,还请具体说明您遇到的问题。该框是否完全没有显示?它不是用于按你期望的方式处理?这将有助于缩小问题的范围。