使用事件处理程序计算的BMI计算器

时间:2013-03-13 19:03:56

标签: c# asp.net event-handling

因此,在使用我的BMI计算器时,我似乎无法使计算部分工作。我有三个文本框,其中包含完成计算所需的信息,英尺高度,英寸高度和磅重。

这是我的代码。什么似乎是我的错?

<form id="US" runat="server" visible="true">
   <div style="background-color:#4DB8FF; width:350px; height:300px; margin:auto; text-align:center; font-family:Arial">
        <h4>
            Body Mass Index Calculator 
            <asp:Button id="btnUS" runat="server" text="US" OnClick="btnUS_Click" /> 
            <asp:Button id="btnMetric" runat="server" Text="Metric" OnClick="btnMetric_Click" /> <br />

            <script runat="server">
                void btnUS_Click(object sender, EventArgs e)
                {
                    this.US.Visible = true;
                    this.Metric.Visible = false;
                }

                void btnMetric_Click(object sender, EventArgs e)
                {
                    this.US.Visible = false;
                    this.Metric.Visible = true;
                }

                void calcUS_Click(object sender, EventArgs e)
                {
                    string operand1 = heightus.Text;
                    string operand2 = heightus1.Text;
                    string operand3 = weightus.Text;

                    string bmi = resultus.Text;

                    double number;

                    bool isOperand1Number = Double.TryParse(operand1, out number);
                    bool isOperand2Number = Double.TryParse(operand2, out number);
                    bool isOperand3Number = Double.TryParse(operand3, out number);


                    if (isOperand1Number && isOperand2Number && isOperand3Number)
                    {
                        bmi = (((operand3 / ((operand1 * 12) + operand2) * (operand1 * 12) + operand2))) * 703);
                    }

                    else
                    {
                        Response.Write("Please type a numeric value into each of the text boxes.");
                    }
                }
            </script>
        </h4>

                  <asp:label ID="lbl1" Text="Height:" runat="server" />
                     <asp:TextBox ID="heightus" runat="server" />feet<br />
                     <asp:TextBox ID="heightus1" runat="server" />inch(es)<br />
                  <asp:Label ID="lbl2" Text="Weight:" runat="server" />
                     <asp:TextBox ID="weightus" runat="server" />lbs<br />
                <br />
                     <asp:Button ID="calcUS" Text="Calculate"  OnClick="calcUS_Click" runat="server" />
                     <asp:Button ID="clearUS" runat="server" Text="Clear"/> 
                <br /><br />
                  <asp:Label ID="lbl3" Text="Results:" runat="server" />
                     <asp:TextBox ID="resultus" runat="server" /> <br />


    </div>
</form>

1 个答案:

答案 0 :(得分:0)

我认为你没有正确地初始化你的isOperand1Number isOperand2Number和isOperand3Number布尔值。放一些调试语句并输出结果,以便您可以看到失败的位置。

您可能还想将强制转换字符串bmi,operand1,2,3键入整数。当您尝试添加它们并在以下位置计算它们时,它们会保留字符串:

        bmi = (((operand3 / ((operand1 * 12) + operand2) * (operand1 * 12) + operand2))) * 703);