C#打印菱形

图形界面:C#打印菱形_第1张图片
打印菱形按钮代码:

 private void button1_Click(object sender, EventArgs e)
        {
           
           int n = Convert.ToInt32(textBox1.Text);  //n菱形行数
            for (int i = 1; i <= n;i++ )    //上半菱形
            {
                for (int a = 1; a <= n-i;a++) //打印空格
                {
                    lblOut.Text += " ";
                }
                for ( int a = 1; a <= (2*i-1); a++) //打印字符
                {    
                    lblOut.Text += Convert.ToChar(64+i);
                }
                lblOut.Text += "\n"; //换行
             }

            for (int i = n-1; i >= 1; i--)    //下半菱形
            {
                for (int a = 1; a <= n - i; a++) //打印空格
                {
                    lblOut.Text += " ";
                }
                for (int a = 1; a <= (2 * i - 1); a++) //打印字符
                {
                    lblOut.Text += Convert.ToChar(64 + i);
                }
                lblOut.Text += "\n";  //换行
            }

        }

你可能感兴趣的:(C#打印菱形)