2013-03-13 197 views
-1

因此,在我的BMI计算器上工作时,似乎无法使计算部分工作。我有三个文本框,其中包含完成计算所需的信息,英尺高度,英寸高度和磅磅。使用事件处理程序的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> 
+0

您能解释一下您面临的问题吗?什么是不工作等,这将有助于解决您的问题。 – Raman 2013-03-13 19:07:24

+0

首先,您正在设置'bmi',但是您并未将该设置设置为页面上的任何内容以查看结果(如'resultus.Text = myResult')。其次,你正在使用字符串进行计算。第三,你应该把所有的代码移到代码隐藏中。第四,只需使用JavaScript! – MikeSmithDev 2013-03-13 19:07:45

回答

0

我不认为你是正确的初始化您isOperand1Number isOperand2Number和isOperand3Number布尔值。输入一些调试语句并输出结果,以便查看失败的位置。

您可能还想将转换字符串bmi,operand1,2,3键入整数。当您尝试添加并计算它们时,它们会保持字符串:

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