使用向后和向前斜线的金字塔

时间:2013-09-04 06:29:32

标签: asp.net

如何编写一个程序,它将采用整数输入,这是金字塔的基本宽度,并使其显示如下: enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

ASPX:

Width:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
        Text="Show pyramid" />
<br /><br />
<asp:Label ID="Label1" runat="server"></asp:Label>

代码背后:

protected void Button1_Click1(object sender, EventArgs e)
{
    Label1.Text = "";
    int width = int.Parse(TextBox1.Text);
    int widthBetween = 4;
    int firstSpace = 5 + width - 1;
    for (int k = 0; k < firstSpace + 2; k++)
    {
         Label1.Text+="&nbsp;";
    }
    Label1.Text += "^<br>";
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < firstSpace; j++)
        {
            Label1.Text += "&nbsp;";
        }
        Label1.Text += "/";
        for (int m = 0; m < widthBetween; m++)
        {
            if (i == width - 1)
            {
                if (m > (widthBetween / 2 - 1))
                    break;
                Label1.Text += "_";
            }
            else
            {
                Label1.Text += "&nbsp;";
            }
        }
        Label1.Text += @"\<br>";
        widthBetween += 2;
        firstSpace--;
    }
}