C#——编写窗体程序,使用for语句输出杨辉三角的前十行

设计界面:一个Label标签

编写代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace _2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            int [,]a=new int [11,11];

            label1.Text = "";

            for (int i = 0; i <= 10;i++ )

            {

                for (int j = 0; j < i;j++ )

                {

                    if (j == 0 || i == j)

                    {

                        a[i, j] = 1;

                    }

                    else 

                    {

                        a[i, j] = a[i - 1, j - 1] + a[i - 1, j];

                    }

                    label1.Text += a[i, j]+" ";

                }

                label1.Text += "\n";

            }

        }

    }

}

运行结果:

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88773355