1.打印三角形

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        public static void Main()
        {
            Console.Write("input the lines number:");
            int lines = int.Parse(Console.ReadLine());

            Console.WriteLine("");

            for (int i = 1; i <= lines; i++)
            {
                for (int k = 1; k <= lines - i; k++)
                {
                    Console.Write(" ");
                }

                for (int j = 1; j <= i * 2 - 1; j++)
                {
                    Console.Write("*");
                }

                Console.WriteLine("");
            }        
           
        }
    }
}

 

2.利用stringbuild实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        public static void Main()
        {
            Console.Write("input the lines number:");         
                   
            int lines = int.Parse(Console.ReadLine()); 
            Console.WriteLine(""); 
            StringBuilder sb = new StringBuilder();

            for (int i = 1; i <= lines; i++)
            {
                sb.Append(' ', lines - i);
                sb.Append('*', i * 2 - 1);
                Console.WriteLine(sb.ToString());
                sb.Remove(0, sb.Length);
            }
        }
    }
}

 

posted on 2012-05-23 10:56  higirle  阅读(138)  评论(0)    收藏  举报