2013-03-13 87 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);