题目:输出n行三角形*号图形。
分析:
1.n行必须有n次循环.
2.占位孔符为n-i(n为总行数,i为当前要打印的行数).
3.要输出的*的个数为2*i-1(i为当前要打印的行数).
程序实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triangle
{
        class Program
        {
                static void Main(string[] args)
                {
                        while (true)
                        {
                                Console.WriteLine("Please put in an angument for create triangle:");
                                
                                try
                                {
                                        string inputstr=Console.ReadLine();
                                        int n = Int32.Parse(inputstr);
                                        if (n < 0) continue;
                                        DrawTriangle(n);
                                }
                                catch (Exception)
                                {
                                        Console.WriteLine("Invalidate argument! Please put in again:");
                                        continue;
                                }
                        }
                }
                static void DrawTriangle(int n)
                {
                        for (int i =1; i <=n; i++)
                        {
                                //输出占位符
                                for (int j = 1; j <=n-i; j++)
                                {
                                        Console.Write(" ");
                                }
                                //输出*
                                for (int j = 1; j <=2*i-1; j++)
                                {
                                        Console.Write("*");
                                }
                                //输出每一行后换行
                                Console.WriteLine();
                        }
                }
        }
}
运行结果: