次のタスクを実行する非常に単純なC#コードを作成しました。
n
スペースの量を表す数値が与えられると、私のプログラムは砂時計を印刷します。
*****
***
*
***
*****
n
コマンドウィンドウの左側の境界壁からの開始スペース。
私のコードは意図したとおりに機能しますが、非常に乱雑でまとまりがなく、より良い方法があると確信しています。おそらく、.NET Frameworkを開発したマイクロソフト開発者によって以前にC#で実装されたものです。
これは私のコードです:
public static void printAsterisk(int initialSpace)
{
int i, j;
char[] asteriskMsg = new char[5 * initialSpace + 25];
for (i = 0; i < initialSpace; i++)
{
asteriskMsg[i] = ' ';
}
for (j = i; j < i + 5; j++)
{
asteriskMsg[j] = '*';
}
asteriskMsg[j] = '\n';
for (i = j + 1; i < j + initialSpace + 1; i++)
{
asteriskMsg[i] = ' ';
}
for (j = i + 1; j < i + 4; j++)
{
asteriskMsg[j] = '*';
}
asteriskMsg[j] = '\n';
for (i = j + 1; i < j + initialSpace + 3; i++)
{
asteriskMsg[i] = ' ';
}
asteriskMsg[i] = '*';
asteriskMsg[i + 1] = '\n';
for (j = i + 2; j < i + initialSpace + 3; j++)
{
asteriskMsg[j] = ' ';
}
for (i = j; i < j + 3; i++)
{
asteriskMsg[i] = '*';
}
asteriskMsg[i] = '\n';
for (j = i + 1; j < i + initialSpace + 1; j++)
{
asteriskMsg[j] = ' ';
}
for (i = j; i < j + 5; i++)
{
asteriskMsg[i] = '*';
}
string s = new string(asteriskMsg);
System.Console.Write("{0}", s);
}
}
かなり恐ろしいです。このコードを改善する方法について非常に興味があります。私が言っていることの例を示すために、これはプログラムがどのように実行されるかですn=25
nがゼロの場合、同じ画像が表示されますが、25個のスペースではなく、左側の壁から始まります。私が何をしようとしているのかが明確になったことを願っています。
編集:文字列を使用して自分で素晴らしいソリューションを思い付くことができました!
public static void printAsterisk(int initialSpace)
{
string firstRow = "*****", secondRow = "***", thirdRow = "*", firstRowSpaces = new string(' ', initialSpace), secondRowSpaces = new string(' ', initialSpace+1), thirdRowSpaces = new string(' ', initialSpace+2);
string hourglass = string.Format("{0}{1}\n{2}{3}\n{4}{5}\n{2}{3}\n{0}{1}", firstRowSpaces, firstRow, secondRowSpaces, secondRow, thirdRowSpaces, thirdRow);
System.Console.WriteLine(hourglass);
}
私はこの質問が好きでした:)
これが私のやり方です。これは本当に楽しいので、本当に他の人を期待しています:)
public class Program
{
public static void Main(string[] args)
{
PrintHourGlass(25);
}
/// <summary>
/// Prints an hour glass starting at the given offset
/// </summary>
/// <param name="indent">The starting offset</param>
static void PrintHourGlass(int indent)
{
PrintAsteriks(indent, 5); // offset = indent, print 5 * [I]*****
PrintAsteriks(indent + 1, 3); // offset = indent + 1, print 3 * [I] ***
PrintAsteriks(indent + 2, 1); // offset = indent +3, print 1 * [I] *
PrintAsteriks(indent + 1, 3); // offset = indent + 1, print 3 * [I] ***
PrintAsteriks(indent, 5); // offset = indent, print 5 * [I]*****
}
/// <summary>
/// Prints given number of * characters starting from the given offset
/// </summary>
/// <param name="indent">The starting offset</param>
/// <param name="asterisks">The number of * characters to print</param>
static void PrintAsteriks(int indent, int asterisks)
{
// Check starters guide to string.Format here:
// https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx#Starting
// string format uses a template and allows you to pass in and format arguments as you like.
// The template below evaluates to {0,25}{1} for indent = 25 and asteriks = 5
// which means,
// print argument 0 (first parameter), no extra formatting, pad the output to 25 characters
// print argument 1, no formatting
string formatString = "{0," + indent + "}{1}";
// Console.WriteLine method uses string.Format internally
// Below, for the template, argument 0 is null
// (since we want to print only 25 characters, the padding.
// The value could have been "" instead of null)
// Argument 1 is a string of * characters, with the length specified by asteriks parameter
Console.WriteLine(formatString, null, new string('*', asterisks));
// Therefore, it outputs 25 linear white spaces, then 5 * characters.
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加