ElseIf声明未达到Msgbox错误

时间:2015-01-07 12:25:36

标签: vb.net if-statement msgbox

输入错误数据时,错误不会运行msgbox

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim weight As Decimal
        Dim height As Decimal
        Dim bmi As Decimal
        weight = InputBox("enter weight")
        height = InputBox("enter height")
        If weight > 10 And weight <= 600 And height >= 0.5 And height <= 2.7 Then
            bmi = weight / (height) ^ 2
        ElseIf weight <= 10 And weight > 600 Then
            MsgBox("you have enterd in valid data it must be above 10 and below or equal 600")
        ElseIf height < 0.5 And height > 2.7 Then
            MsgBox("you have enterd in valid data it must between 0.5 and 2.7 inclusive")
        End If
        TextBox1.Text = bmi
End Sub

2 个答案:

答案 0 :(得分:2)

您的错误在这里

ElseIf weight <= 10 And weight > 600 Then
        MsgBox("you have enterd in valid data it must be above 10 and below or equal 600")
    ElseIf height < 0.5 And height > 2.7 Then
        MsgBox("you have enterd in valid data it must between 0.5 and 2.7 inclusive")
    End If

您的体重永远不会小于或等于10且大于600。同样适用于高度

您需要使用OR代替AND

答案 1 :(得分:0)

请检查您的条件。 没有价值可以低于10且高于600。 例如。让我们取weigth = 3。 它小于10但不超过600。

请更正您的条件。