| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4676 人关注过本帖
标题:用C#编写一个金字塔!
只看楼主 加入收藏
mjx5
Rank: 2
等 级:论坛游民
威 望:1
帖 子:62
专家分:47
注 册:2010-3-22
收藏
得分:0 
static void Main(string[] args)
        {
            jinzita da = new jinzita();
            da.fa();
            Console.ReadLine();
        }

    }
    class jinzita
    {
        public void fa()
        {
            Console.WriteLine("请输入一个一到九的整数");
            int n = 0;
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("您输入的数是:" + n);
            //遍历行
            for (int hang = 1; hang <= n; hang++)
            {
                //留空格
                for (int count = 1; count <= n - hang; count++)
                {
                    Console.Write(" ");
                }
                //写左边数字
                for (int i = 1; i <= hang; i++)
                {
                    Console.Write(i + " ");
                }
                if (hang == 1)
                {
                    Console.WriteLine("");
                }
                else
                {
                    //写右边数字
                    for (int j = hang - 1; j >= 1; j--)
                    {
                        Console.Write(j + " ");
                        if (j == 1)
                        {
                            Console.WriteLine("");
                        }
                    }
                }
            }
        }
2010-03-23 09:51
mjx5
Rank: 2
等 级:论坛游民
威 望:1
帖 子:62
专家分:47
注 册:2010-3-22
收藏
得分:0 
static void Main(string[] args)
        {
            jinzita da = new jinzita();
            da.fa();
            Console.ReadLine();
        }

    }
    class jinzita
    {
        public void fa()
        {
            Console.WriteLine("请输入一个一到九的整数");
            int n = 0;
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("您输入的数是:" + n);
            //遍历行
            for (int hang = 1; hang <= n; hang++)
            {
                //留空格
                for (int count = 1; count <= n - hang; count++)
                {
                    Console.Write(" ");
                }
                //写左边数字
                for (int i = 1; i <= hang; i++)
                {
                    Console.Write(i + " ");
                }
                if (hang == 1)
                {
                    Console.WriteLine("");
                }
                else
                {
                    //写右边数字
                    for (int j = hang - 1; j >= 1; j--)
                    {
                        Console.Write(j + " ");
                        if (j == 1)
                        {
                            Console.WriteLine("");
                        }
                    }
                }
            }
        }
2010-03-23 09:54
快速回复:用C#编写一个金字塔!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047480 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved