前段時間,Insus.NET有實現一組字元串在輸出時,靠左或靠右對齊。《輸出的字元靠右對齊》http://www.cnblogs.com/insus/p/7953304.html 現在Insus.NET參考此方法,實一張塔松葉,實現之前,先練習一下,輸出半張: public void WriteTr ...
前段時間,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); } }Source Code
目標實現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); } }Source Code
在控制台,執行代碼: