我正在编写一个非常简单的重量计算器代码 - 用户输入重量和高度,我计算该高度的标准重量,代码根据if / elseif中的标准将该重量与重量范围进行比较块。
正确返回标准重量,但代码始终返回"正常重量",无论高度重量比如何。我是VB的新手,所以我的预感是一个相对简单的语法问题。
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
lblFeedback.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight <= dbStWeight * 1.1) Then
lblResult.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 < dbWeight <= dbStWeight * 1.2) Then
lblResult.Text = ("Over Weight")
ElseIf (dbStWeight * 0.8 <= dbWeight < dbStWeight * 0.9) Then
lblResult.Text = ("Under Weight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
lblResult.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
lblResult.Text = ("Very underweight")
End If
lblFeedback.Refresh()
lblResult.Refresh()
答案 0 :(得分:0)
Private Sub btOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
StWeight.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.1) Then
Result.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.2) Then
Result.Text = ("Overweight")
ElseIf (dbStWeight <= dbWeight) AndAlso (dbWeight < dbStWeight * 0.9) Then
Result.Text = ("Underweight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
Result.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
Result.Text = ("Very underweight")
End If
End Sub