为什么我的程序的结果总是体重不足?

时间:2018-09-10 10:28:14

标签: vb.net

这是我的代码:

Dim Weight, Height, Bmi_value As Integer
    Weight = TextBox1.Text
    Height = TextBox2.Text

    Bmi_value = (Weight / Height ^ 2)
    TextBox3.Text = Bmi_value

    Select Case Bmi_value
        Case 0.0 To 18.5
            TextBox4.Text = "Underweight"
        Case 18.6 To 24.9
            TextBox4.Text = "Normal"
        Case 25.0 To 29.9
            TextBox4.Text = "Overweight"
        Case Is >= 30.0
            TextBox4.Text = "Obese"

    End Select
End Sub

2 个答案:

答案 0 :(得分:1)

这是您的固定代码:

'You were using Integer instead of Double
Dim Weight, Height, Bmi_value As Double
If you want to take the value from the textBoxes, you have to covert it to Double.
Weight = Convert.ToDouble(TextBox1.Text)
Height = Convert.ToDouble(TextBox2.Text)

'Better to call the right function Math.Pow()
Bmi_value = (Weight / Math.Pow(Height, 2))

'You have to convert it to String
TextBox3.Text = Convert.ToString(Bmi_value)
Select Case Bmi_value
    Case 0.0 To 18.5
        TextBox4.Text = "Underweight"
    Case 18.6 To 24.9
        TextBox4.Text = "Normal"
    Case 25.0 To 29.9
        TextBox4.Text = "Overweight"
    Case Is >= 30.0
        TextBox4.Text = "Obese"
End Select
End Sub

此代码中有很多错误:

第一:如需要BMI计算一样,如果需要十进制值,请不要将var声明为整数。

第二:如果从TextBox中获取值或要在TextBox中打印它们,请始终将其值转换为正确的类型

答案 1 :(得分:0)

  Dim dblWeight As Double, dblHeight As Double, dblBMI As Double

    dblWeight = CDbl(TextBox1.Text) 'assumes lbs.
    dblHeight = CDbl(TextBox2.Text) 'assumes inches
    On Error GoTo 0
    dblBMI = ((dblWeight * 703.0#) / (dblHeight * dblHeight))
    TextBox3.Text = dblBMI
    Select Case dblBMI
        Case Is <= 18.5 : TextBox4.Text = "Underweight"
        Case 18.6 To 24.9 : TextBox4.Text = "Normal"
        Case 25.0# To 29.9 : TextBox4.Text = "Overweight"
        Case 30.0# To 34.9 : TextBox4.Text = "Obese Class I"
        Case 35.0# To 39.9 : TextBox4.Text = "Obese Class II"
        Case Is >= 40.0# : TextBox4.Text = "Obese Class III"
    End Select

    Exit Sub

尝试一下,为我工作。