C#达成一张塔松叶

前段时间,Insus.NET有落到实处一组字符串在输出时,靠左或靠右对齐。《出口的字符靠右对齐http://www.cnblogs.com/insus/p/7953304.html

近期Insus.NET参考此方法,实一张塔松叶,完成从前,先练习一下,输出半张:
C# 1

 

C# 2C# 3

 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

 

运作代码,看看输了啥样子?
C# 4

 

 

目的落实六分之三。接下来,完毕其它二只,Insus.NET创制其它2个方法:
C# 5

C# 6C# 7

 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

C#, 

在控制台,执行代码:

C# 8

 

相关文章