我假设x是我要分析的数字,“y”是“男性”或“女性”,“z”是年龄组。 x是一个数字,结果将是“体重不足”,“健康”或“超重”。但对于男性和女性来说,实际上是BPI的x并不相同。例如,如果x = 21则取决于性别和年龄,它将被归类为“体重不足”,“健康”或“超重”。
Function BAI(x As Integer, y As String, z As Integer) As String
If x < 21 And y = "female" And 20 <= z <= 39 Then
BAI = "UNDERWEIGHT"
Elseif 22 <= x <= 33 And y = "Female" And 20 <= z <= 39 Then
BAI = "Healthy"
ElseIf 34 <= x <= 38 And y = "Female" And 20 <= z <= 39 Then
BAI = "overweight"
ElseIf x >= 39 And y = "Female" And 20 <= z <= 39 Then
BAI = "OBESE"
ElseIf x <= 23 And y = "female" And 40 <= z <= 59 Then
BAI = "UNDERWEIGHT"
ElseIf 24 <= x <= 35 And y = "female" And 40 <= z <= 59 Then
BAI = "Healthy"
ElseIf 36 <= x <= 41 And y = "female" And 40 <= z <= 59 Then
BAI = "Overweight"
ElseIf x >= 42 And y = "female" And 40 <= z <= 59 Then
BAI = "OBESE"
ElseIf x <= 25 And y = "female" And 60 <= z <= 79 Then
BAI = "UNDERWEIGHT"
ElseIf 26 <= x <= 38 And y = "female" And 60 <= z <= 79 Then
BAI = "Healthy"
ElseIf 39 <= x <= 43 And y = "female" And 60 <= z <= 79 Then
BAI = "Overweight"
ElseIf x >= 44 And y = "female" And 60 <= z <= 79 Then
BAI = "obese"
ElseIf x <= 8 And y = "male" And 20 <= z <= 39 Then
BAI = "Underweight"
ElseIf 9 <= x <= 21 And y = "male" And 20 <= z <= 39 Then
BAI = "Healthy"
ElseIf 22 <= x <= 26 And y = "male" And 20 <= z <= 39 Then
BAI = "overweight"
ElseIf x >= 27 And y = "male" And 20 <= z <= 39 Then
BAI = "OBESE"
ElseIf x <= 11 And y = "male" And 40 <= z <= 59 Then
BAI = "UNDERWEIGHT"
ElseIf 12 <= x <= 23 And y = "male" And 40 <= z <= 59 Then
BAI = "Healthy"
ElseIf 24 <= x <= 28 And y = "male" And 40 <= z <= 59 Then
BAI = "Overweight"
ElseIf x >= 29 And y = "male" And 40 <= z <= 59 Then
BAI = "OBESE"
ElseIf x <= 13 And y = "male" And 60 <= z <= 79 Then
BAI = "UNDERWEIGHT"
ElseIf 14 <= x <= 25 And y = "male" And 60 <= z <= 79 Then
BAI = "Healthy"
ElseIf 26 <= x <= 30 And y = "male" And 60 <= z <= 79 Then
BAI = "Overweight"
End If
End Function
答案 0 :(得分:0)
这是编辑问题的新答案。
如果您要去If-Then-Else路线,那么我建议将您的逻辑分解成组。我试过了,但我对你的领域并不熟悉,所以我可能没有完美地完成它:
另外,为了便于阅读,我建议将变量重命名为更有意义的内容。我将“x”重命名为“BPI”,将“y”重命名为“Gender”,将“z”重命名为“Age”。
编辑:对于区分大小写的“男性”和“女性”,我已将UCase
函数添加到下面的代码中。它将字符串转换为全部大写,从而可以将其与全部大写字符串进行比较。例如,“MaLe”将转换为“MALE”,然后与“MALE”进行比较。换句话说,这应该从函数的性别部分消除区分大小写。
此外,对于性别之前/之后的可能空格,使用Trim
功能将消除它们。因此,Trim("Male ")
变为"Male"
(无空格)。
请查看以下内容,看看它是否更符合您的要求:
Function BAI(BPI As Integer, Gender As String, Age As Integer) As String
If UCase(Trim(Gender)) = "FEMALE" Then
If 20 <= Age And Age <= 39 Then
If BPI <= 21 Then BAI = "UNDERWEIGHT"
If 22 <= BPI And BPI <= 33 Then BAI = "Healthy"
If 34 <= BPI And BPI <= 38 Then BAI = "Overweight"
If BPI >= 39 Then BAI = "OBESE"
ElseIf 40 <= Age And Age <= 59 Then
If BPI <= 23 Then BAI = "UNDERWEIGHT"
If 24 <= BPI And BPI <= 35 Then BAI = "Healthy"
If 36 <= BPI And BPI <= 41 Then BAI = "Overweight"
If BPI >= 42 Then BAI = "OBESE"
ElseIf 60 <= Age And Age <= 79 Then
If BPI <= 25 Then BAI = "UNDERWEIGHT"
If 26 <= BPI And BPI <= 38 Then BAI = "Healthy"
If 39 <= BPI And BPI <= 43 Then BAI = "Overweight"
If BPI >= 44 Then BAI = "OBESE"
Else
BAI = "Error - Age outside acceptable range"
End If
ElseIf UCase(Trim(Gender)) = "MALE" Then
If 20 <= Age And Age <= 39 Then
If BPI <= 8 Then BAI = "UNDERWEIGHT"
If 9 <= BPI And BPI <= 21 Then BAI = "Healthy"
If 22 <= BPI And BPI <= 26 Then BAI = "Overweight"
If BPI >= 27 Then BAI = "OBESE"
ElseIf 40 <= Age And Age <= 59 Then
If BPI <= 11 Then BAI = "UNDERWEIGHT"
If 12 <= BPI And BPI <= 23 Then BAI = "Healthy"
If 24 <= BPI And BPI <= 28 Then BAI = "Overweight"
If BPI >= 29 Then BAI = "OBESE"
ElseIf 60 <= Age And Age <= 79 Then
If BPI <= 13 Then BAI = "UNDERWEIGHT"
If 14 <= BPI And BPI <= 25 Then BAI = "Healthy"
If 26 <= BPI And BPI <= 30 Then BAI = "Overweight"
If BPI >= 31 Then BAI = "OBESE"
Else
BAI = "Error - Age outside acceptable range"
End If
Else
BAI = "Error - Choose male or female"
End If
End Function