如题所述:一个由*号组成的4行倒三角形图案。要求:1、输入倒三角形的行数,行数的取值3-21之间,对于非法的行数,要求抛出提示“非法行数!”;2、在屏幕上打印这个指定了行数的倒三角形。
     *******
     *****
      ***
      *感觉自己基础太差了,还请高人指点

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 0;
                Console.Write("输入倒三角形的行数,行数的取值3-21之间:");
                try
                {
                    n = int.Parse(Console.ReadLine());
                }
                catch { }
                if (n < 3 || n > 21)
                {
                    Console.WriteLine("非法行数!");
                    return;
                }
                for (int i = n; i >= 1; i--)
                {
                    Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1));
                }
                Console.ReadKey();
            }
        }
    }
      

  2.   

    果然厉害。。啊
    下面是菜鸟的代码for(int row=1;row<=rowmax;row++)
    {
       w(g(row,rowmax));
    }public string g(int rownum, int rowmax)
    {     string star=string.Empty;
         int starnum= (rownum*2-1);
         star=star+ds(" ",(rowmax-rownum)/2);
         star=star+ds("*",starnum);
         star=star+ds(" ",(rowmax-rownum)/2);
         return star;
    }
    public static string ds(string src,int num)
    {
         for(int i=0;i<num;i++)
        {
           src++;
        }
       return src;
    }public static void w(string str)
    {
       Console.WriteLine(str);
    }
      

  3.   

    Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1));
    这句代码高高深了,有些看不懂
      

  4.   

    方法好多,怕你不懂
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 0;
                Console.Write("输入倒三角形的行数,行数的取值3-21之间:");
                try
                {
                    n = int.Parse(Console.ReadLine());
                }
                catch { }
                if (n >= 3 && n <= 21)
                    Console.WriteLine(string.Join("\r\n", Enumerable.Range(1, n).Select(x => new string(' ', x) + new string('*', (n - x) * 2 + 1)).ToArray()));
                else
                    Console.WriteLine("非法行数!");
            }
        }
    }
      

  5.   

    本帖最后由 caozhy 于 2012-09-28 23:58:44 编辑
      

  6.   

    这种题目要举一反三啦前两天有一个人问 http://topic.csdn.net/u/20120911/18/c3989ad7-6e98-43f8-992c-e1aec7a966dc.html
    都是一回事。