如何更改IF语句以选择....案例

时间:2011-12-17 08:59:36

标签: vb.net

我得到了以下具有特定变量的多个值的以下内容,我不想堆叠到很多IF ELSE语句来处理它们,所以如果有的话我会很感激可以使用SELECT CASE语句非常友好地修改代码。

此外,我希望dgMarksEntry.Item(7, i).ValuedgMarksEntry.Item(8, i).Value分别处理RANKCOUNT项目输入的数量。

 For i = 0 To 100
        dgMarksEntry.Item(4, i).Value = Val(dgMarksEntry.Item(3, i).Value) + Val(dgMarksEntry.Item(2, i).Value)

        If dgMarksEntry.Item(4, i).Value >= 80 Then
            dgMarksEntry.Item(5, i).Value = "A1"
            dgMarksEntry.Item(6, i).Value = "Excellent"
        ElseIf dgMarksEntry.Item(4, i).Value >= 75 Then
            dgMarksEntry.Item(5, i).Value = "B2"
            dgMarksEntry.Item(6, i).Value = "Very good"
        ElseIf dgMarksEntry.Item(4, i).Value >= 70 Then
            dgMarksEntry.Item(5, i).Value = "B3"
            dgMarksEntry.Item(6, i).Value = "Good"
        ElseIf dgMarksEntry.Item(4, i).Value >= 65 Then
            dgMarksEntry.Item(5, i).Value = "C4"
            dgMarksEntry.Item(6, i).Value = "Credit"
        ElseIf dgMarksEntry.Item(4, i).Value >= 60 Then
            dgMarksEntry.Item(5, i).Value = "C5"
            dgMarksEntry.Item(6, i).Value = "Credit"
        ElseIf dgMarksEntry.Item(4, i).Value >= 55 Then
            dgMarksEntry.Item(5, i).Value = "C6"
            dgMarksEntry.Item(6, i).Value = "Credit"
        ElseIf dgMarksEntry.Item(4, i).Value >= 50 Then
            dgMarksEntry.Item(5, i).Value = "D7"
            dgMarksEntry.Item(6, i).Value = "Pass"
        ElseIf dgMarksEntry.Item(4, i).Value >= 40 Then
            dgMarksEntry.Item(5, i).Value = "E8"
            dgMarksEntry.Item(6, i).Value = "Weak pass"
        ElseIf dgMarksEntry.Item(4, i).Value <= 39 Then
            dgMarksEntry.Item(5, i).Value = "F9"
            dgMarksEntry.Item(6, i).Value = "Fail"
        End If
    Next

2 个答案:

答案 0 :(得分:1)

您需要做的一切都在这个页面上,包括一些让您入门的例子。

http://msdn.microsoft.com/en-us/library/cy37t14y.aspx

答案 1 :(得分:1)

我会选择一种完全不同的方法。首先,声明一个可以包含评级信息的类:

Public Class CreditRating
    Private m_Limit As Integer
    Public Property Limit() As Integer
        Get
            Return m_Limit
        End Get
        Set(ByVal value As Integer)
            m_Limit = value
        End Set
    End Property

    Private m_Rating As String
    Public Property Rating() As String
        Get
            Return m_Rating
        End Get
        Set(ByVal value As String)
            m_Rating = value
        End Set
    End Property

    Private m_Appreciation As String
    Public Property Appreciation() As String
        Get
            Return m_Appreciation
        End Get
        Set(ByVal value As String)
            m_Appreciation = value
        End Set
    End Property
End Class

然后将您的逻辑更改为:

Dim ratings As New List(Of CreditRating)

ratings.Add(New CreditRating With {.Limit = 80, .Rating = "A1", .Appreciation = "Excellent"})
ratings.Add(New CreditRating With {.Limit = 75, .Rating = "B2", .Appreciation = "Very good"})
ratings.Add(New CreditRating With {.Limit = 70, .Rating = "B3", .Appreciation = "Good"})
ratings.Add(New CreditRating With {.Limit = 65, .Rating = "C4", .Appreciation = "Credit"})
ratings.Add(New CreditRating With {.Limit = 60, .Rating = "C5", .Appreciation = "Credit"})
ratings.Add(New CreditRating With {.Limit = 55, .Rating = "C6", .Appreciation = "Credit"})
ratings.Add(New CreditRating With {.Limit = 50, .Rating = "D7", .Appreciation = "Pass"})
ratings.Add(New CreditRating With {.Limit = 40, .Rating = "E8", .Appreciation = "Weak pass"})
ratings.Add(New CreditRating With {.Limit = 0, .Rating = "F9", .Appreciation = "Fail"})

For i As Integer = 0 To 100
    Dim value As Double = Conversion.Val(dgMarksEntry(3, i).Value) + Conversion.Val(dgMarksEntry(2, i).Value)
    dgMarksEntry(4, i).Value = value
    For Each rating As CreditRating In ratings
        If value >= rating.Limit Then
            dgMarksEntry(5, i).Value = rating.Rating
            dgMarksEntry(6, i).Value = rating.Appreciation
            Exit For
        End If
    Next
Next

使用此方法,您还可以从文件或数据库中读取评级信息。这样可以更轻松地更改限制。它也不易出错,因为逻辑只编程一次,并且对于所有限制都是相同的。