您的位置:程序门 -> .net技术 -> c#



关于c#语言,大家帮帮忙!!急,正在学c#语言!!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于c#语言,大家帮帮忙!!急,正在学c#语言!![已结贴,结贴人:lhh092020]
发表于:2007-03-17 18:05:23 楼主
用c#写一段代码,最终输出以下三角形

                                          *
                                        *   *
                                      *       *
                                    *           *
                                  *               *
                                ***********  

发表于:2007-03-17 18:07:261楼 得分:0
中断是用户输入,如果输入是5,就打出5行的”*“号三角行
发表于:2007-03-17 18:56:292楼 得分:20
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();
                }
        }
}
发表于:2007-03-17 19:23:053楼 得分:0
建议看看:http://webxz.com.cn/article/showclass.asp?classid=78
发表于:2007-03-17 19:53:004楼 得分:0
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 ");
}
}
}
}


快速检索

最新资讯
热门点击