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