如果声明不会继续

时间:2015-04-29 00:00:27

标签: vb.net if-statement

我正在尝试在VB.net上进行一些计算。我使用了if else语句,因为我对它有点熟悉。我的代码就像这样

 Try
        Dim a As Integer = msalary.Text
        If (a < 9000) Then
            Label5.Text = a - 200
        ElseIf (9000 < a < 9999.99) Then
            Label5.Text = a - 225
        ElseIf (10000 < a < 10999.99) Then
            Label5.Text = a - 250
        ElseIf (11000 <= a < 11999.99) Then
            Label5.Text = a - 275
        ElseIf (12000 <= a < 12999.99) Then
            Label5.Text = a - 300
        ElseIf (13000 <= a < 14000) Then
            Label5.Text = a - 325
        ElseIf (14000 <= a < 15000) Then
            Label5.Text = a - 350
        ElseIf (15000 <= a < 16000) Then
            Label5.Text = a - 375
        ElseIf (17000 <= a < 18000) Then
            Label5.Text = a - 400
        ElseIf (18000 <= a < 19000) Then
            Label5.Text = a - 425
        ElseIf (19000 <= a < 20000) Then
            Label5.Text = a - 450
        ElseIf (20000 <= a < 21000) Then
            Label5.Text = a - 475
        ElseIf (21000 <= a < 22000) Then
            Label5.Text = a - 500
        ElseIf (22000 <= a < 23000) Then
            Label5.Text = a - 525
        ElseIf (23000 <= a < 24000) Then
            Label5.Text = a - 550
        ElseIf (24000 <= a < 25000) Then
            Label5.Text = a - 575
        ElseIf (25000 <= a < 26000) Then
            Label5.Text = a - 600
        ElseIf (26000 <= a < 27000) Then
            Label5.Text = a - 625
        ElseIf (27000 <= a < 28000) Then
            Label5.Text = a - 650
        ElseIf (28000 <= a < 29000) Then
            Label5.Text = a - 675
        ElseIf (29000 <= a < 30000) Then
            Label5.Text = a - 700
        ElseIf (30000 <= a < 31000) Then
            Label5.Text = a - 725
        ElseIf (31000 <= a < 32000) Then
            Label5.Text = a - 750
        ElseIf (32000 <= a < 33000) Then
            Label5.Text = a - 800
        ElseIf (33000 <= a < 34000) Then
            Label5.Text = a - 825
        ElseIf (34000 <= a < 35000) Then
            Label5.Text = a - 850
        ElseIf (a >= 35000) Then
            Label5.Text = a - 875
        ElseIf a = "" Then

        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

即使我把20000放入其中,最远的条件也能达到-225。它只会从20000减去225.我做了什么或者有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

您的AndAlso语句中似乎遗漏了一些If/ElseIf。通常,如果不进行转换,您将无法从String转换为Integer,所以我猜你没有Option Strict

Try
        Dim a As Integer = msalary.Text
        If (a < 9000) Then
            Label5.Text = a - 200
        ElseIf (9000 <= a AndAlso a < 10000) Then
            Label5.Text = a - 225
        ElseIf (10000 <= a AndAlso a < 11000) Then
            Label5.Text = a - 250
        ElseIf (11000 <= a AndAlso a < 12000) Then
            Label5.Text = a - 275
        ElseIf (12000 <= a AndAlso a 13000) Then
            Label5.Text = a - 300
        ElseIf (13000 <= a AndAlso a < 14000) Then
            Label5.Text = a - 325
        ElseIf (14000 <= a AndAlso a < 15000) Then
            Label5.Text = a - 350
        ElseIf (15000 <= a AndAlso a < 16000) Then
            Label5.Text = a - 375
        ElseIf (17000 <= a AndAlso a < 18000) Then
            Label5.Text = a - 400
        ElseIf (18000 <= a AndAlso a < 19000) Then
            Label5.Text = a - 425
        ElseIf (19000 <= a AndAlso a < 20000) Then
            Label5.Text = a - 450
        ElseIf (20000 <= a AndAlso a < 21000) Then
            Label5.Text = a - 475
        ElseIf (21000 <= a AndAlso a < 22000) Then
            Label5.Text = a - 500
        ElseIf (22000 <= a AndAlso a < 23000) Then
            Label5.Text = a - 525
        ElseIf (23000 <= a AndAlso a < 24000) Then
            Label5.Text = a - 550
        ElseIf (24000 <= a AndAlso a < 25000) Then
            Label5.Text = a - 575
        ElseIf (25000 <= a AndAlso a < 26000) Then
            Label5.Text = a - 600
        ElseIf (26000 <= a AndAlso a < 27000) Then
            Label5.Text = a - 625
        ElseIf (27000 <= a AndAlso a < 28000) Then
            Label5.Text = a - 650
        ElseIf (28000 <= a AndAlso a < 29000) Then
            Label5.Text = a - 675
        ElseIf (29000 <= a AndAlso a < 30000) Then
            Label5.Text = a - 700
        ElseIf (30000 <= a AndAlso a < 31000) Then
            Label5.Text = a - 725
        ElseIf (31000 <= a AndAlso a < 32000) Then
            Label5.Text = a - 750
        ElseIf (32000 <= a AndAlso a < 33000) Then
            Label5.Text = a - 800
        ElseIf (33000 <= a AndAlso a < 34000) Then
            Label5.Text = a - 825
        ElseIf (34000 <= a AndAlso a < 35000) Then
            Label5.Text = a - 850
        ElseIf (a >= 35000) Then
            Label5.Text = a - 875
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

答案 1 :(得分:1)

我不会使用这样一个巨大的条件分支,我会找到更多的算法方法。

示例:

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim salary As Integer = 34567, subtractor As Integer = 200
        Dim r As Range = GetSalaryRange(salary)
        For I As Integer = 9000 To r.Lower Step 1000
            subtractor += 25
        Next
        Label5.Text = (salary - subtractor).ToString
    End Sub
    Function GetSalaryRange(salary As Integer) As Range
        If salary < 9000 Then Return New Range() With {.Lower = 0, .Upper = 8999}
        Dim remainder As Integer = salary Mod 1000
        Return New Range With {.Lower = salary - remainder, .Upper = salary - remainder + 999}
    End Function
    Public Class Range
        Public Lower, Upper As Integer
        Public Function Contains(number As Integer) As Boolean
            If number >= Lower AndAlso number <= Upper Then Return True Else Return False
        End Function
    End Class
End Class