实例3_9 循环语句的嵌套

题目描述 

创建一个C#Windows应用程序,输入三角形行数,打印如图所示效果。


1>首先在Windows窗体中添加2个Label控件,1个TextBox,和1个Button控件。各控件的主要属性设置如下表。

控件 属性 属性设置 控件 属性 属性设置
label1 Text 三角形行数: textBox1 Name txtNum
label2 Text "" button1 Name btnOk

Name lbShow
Text 打印

2>在窗体设计区中双击“btnOk”按钮,系统自动为按钮添加“Click”事件及对应的事件方法,然后在源代码视图中编辑代码

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 Test3_9
{
    public partial class Test3_9 : Form
    {
        public Test3_9()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(txtNum.Text);
            StringBuilder sb = new StringBuilder();
            int i, j;
            for (i = 0; i <= n; i++)
            {
                for (j = 1; j <= n - i; j++)
                {
                    sb.Append(" ");
                }
                for (j = 1; j <= 2 * i - 1; j++)
                {
                    sb.Append("*");
                }
                sb.Append("\n");
            }
            lblShow.Text = sb.ToString();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/WYJ____/article/details/80068982