用C#写一段代码,最终输出以下三角形                     *
                    * *
                   *   *
                  *     *
                 *       *
                *********** 

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
                    string numstr = Console.ReadLine();
                    try
                    {
                        int num = Int32.Parse(numstr);
                        Console.Write(GetPrintString(num));
                    }
                    catch {
                        break;
                    }
                }
            }
            static string GetPrintString(int a) {
                if (a <= 0) { return ""; }
                if (a == 1) { return "*"; }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < a; i++)
                {
                    string left = "";//左边的空格
                    for (int j = i; j < a - 1; j++)
                    {
                        left += " ";
                    }
                    if (i == 0)
                    {
                        sb.AppendFormat("{0}*\r\n", left);
                        continue;
                    }
                    string middle = "";
                    middle += i == a - 1 ? "*" : " ";
                    for (int j = 0; j < i; j++) 
                    {
                        if (j > 0) middle += i == a - 1 ? "**" : "  ";
                    }
                    sb.AppendFormat("{0}*{1}*\r\n", left, middle);
                }
                return sb.ToString();
            }
        }
    }
      

  2.   

    建议看看:http://webxz.com.cn/Article/ShowClass.asp?ClassID=78
      

  3.   

    using System;
    using System.Text;namespace ConsoleApplication5
    {
    class Program
    {
    static void Main(string[] args)
    {
    int i,j,k;
    for(i=1;i<=4;i++)
    {
    for(j=1;j<=4-i;j++)
    {
    System.Console.WriteLine(" ");
    }
    for(k=1;k<=2*i-1;k++)
    {
    System.Console.WriteLine("*");
    }
    System.Console.WriteLine("\n");
    }
    }
    }
    }