VB计算BMI指数

2025-04-13 21:33:30
推荐回答(3个)
回答(1):

你的公式不对a/(b*2),我帮你改成a/(b*1.6),看起来差不多,实际公式是a=b-1.05是标准身材。 Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
a = Val(Text1)
b = Val(Text2)
c = Round(a / (b * 1.6))
Label6.Caption = c
If Option1.Value Then
If c < 20 Then
Label8.Caption = "过轻"
ElseIf c < 25 Then
Label8.Caption = "适中"
ElseIf c < 30 Then
Label8.Caption = "过重"
ElseIf c < 35 Then
Label8.Caption = "肥胖"
ElseIf c >= 35 Then
Label8.Caption = "非常肥胖"
End If
Else
If c < 19 Then
Label8.Caption = "过轻"
ElseIf c < 24 Then
Label8.Caption = "适中"
ElseIf c < 29 Then
Label8.Caption = "过重"
ElseIf c < 34 Then
Label8.Caption = "肥胖"
ElseIf c >= 34 Then
Label8.Caption = "非常肥胖"
End If
End If
End Sub

回答(2):

并不是公式的问题哒!溢出是因为变量定义出错。修改代码:Dim a As Single, b As Single, c As Single放通用段。Private Sub Command1_Click()
a = Val(Text1)
b = Val(Text2)
If Option1.Value Then
c = Round(a / (b * 1.6))
Label3.Caption = c
If c < 20 Then Label4.Caption = "过轻"
If 20 <= c < 25 Then Label4.Caption = "适中"
If 25 <= c < 30 Then Label4.Caption = "过重"
If 30 <= c < 35 Then Label4.Caption = "肥胖"
If 35 >= c Then Label4.Caption = "非常肥胖"
End If
If Option2.Value Then
c = Round(a / (b * 1.6))
Label3.Caption = c
If c < 19 Then Label4.Caption = "过轻"
If 19 <= c < 24 Then Label4.Caption = "适中"
If 24 <= c < 29 Then Label4.Caption = "过重"
If 29 <= c < 34 Then Label4.Caption = "肥胖"
If 34 >= c Then Label4.Caption = "非常肥胖"
End If
End Sub

回答(3):

首先对两个TEXT中的值进行判断,判断是否是数值,再判断数值是否是>0,且身高<2.5,体重<500,这个就能帮助得到比较准确的值,防止乱填。