前段时间,Insus.NET有实现一组字符串在输出时,靠左或靠右对齐。《输出的字符靠右对齐》http://www.cnblogs.com/insus/p/7953304.html
现在Insus.NET参考此方法,实一张塔松叶,实现之前,先练习一下,输出半张:
public void WriteTriangle()
{
char symbol = '*';
int star = ;
for (var i = ; i <= star; i++)
{
string s = new string(symbol, i);
s = s.PadLeft(star, ' ');
Console.WriteLine(s);
}
}
Source Code
运行代码,看看输了啥样子?
目标实现50%。接下来,完成另外一边,Insus.NET创建另外一个方法:
public void WritePineTree()
{
char symbol = '*';
int star = ; Console.WriteLine(symbol.ToString().PadLeft(star + , ' '));
for (var i = ; i <= star; i++)
{
string s = new string(symbol, i);
string left = s.PadLeft(star, ' ');
string right = s.PadRight(star, ' ');
Console.WriteLine(left + symbol.ToString() + right);
}
}
Source Code
在控制台,执行代码: