您可以使用自定義的Range
類和Dicitonary
來存儲範圍+值:
Function GetValue(number As Int32) As Int32
Dim rangeValues = New Dictionary(Of Range(Of Int32), Int32)
rangeValues.Add(New Range(Of Int32)(1000), 20)
rangeValues.Add(New Range(Of Int32)(1001, 2000), 40)
rangeValues.Add(New Range(Of Int32)(2001, 3000), 60)
rangeValues.Add(New Range(Of Int32)(3001, 4000), 80)
rangeValues.Add(New Range(Of Int32)(4001, 5000), 40)
rangeValues.Add(New Range(Of Int32)(5001, 6000), 40)
rangeValues.Add(New Range(Of Int32)(6001, 7000), 60)
rangeValues.Add(New Range(Of Int32)(7001, 8000), 80)
rangeValues.Add(New Range(Of Int32)(8001, 9000), 100)
rangeValues.Add(New Range(Of Int32)(9001, 10000), 120)
rangeValues.Add(New Range(Of Int32)(10001, 11000), 140)
rangeValues.Add(New Range(Of Int32)(11001, 12000), 160)
rangeValues.Add(New Range(Of Int32)(12001, 13000), 180)
rangeValues.Add(New Range(Of Int32)(13001, 14000), 200)
rangeValues.Add(New Range(Of Int32)(14001, 15000), 220)
Dim firstMatchingRange = rangeValues.Keys.
FirstOrDefault(Function(r) r.Minimum <= number AndAlso r.Maximum >= number)
If firstMatchingRange IsNot Nothing Then
Return rangeValues(firstMatchingRange)
Else
Throw New ArgumentException("invalid number", "number")
End If
End Function
這裏的Range
類
Public Class Range(Of T As IComparable(Of T))
Public Sub New()
End Sub
Public Sub New(minimum As T, maximum As T)
minimum = minimum
maximum = maximum
End Sub
Public Sub New(maximum As T)
maximum = maximum
End Sub
''' <summary>
''' Minimum value of the range
''' </summary>
Public Property Minimum() As T
Get
Return m_Minimum
End Get
Set(value As T)
m_Minimum = value
End Set
End Property
Private m_Minimum As T
''' <summary>
''' Maximum value of the range
''' </summary>
Public Property Maximum() As T
Get
Return m_Maximum
End Get
Set(value As T)
m_Maximum = value
End Set
End Property
Private m_Maximum As T
''' <summary>
''' Presents the Range in readable format
''' </summary>
''' <returns>String representation of the Range</returns>
Public Overrides Function ToString() As String
Return [String].Format("[{0} - {1}]", Minimum, Maximum)
End Function
''' <summary>
''' Determines if the range is valid
''' </summary>
''' <returns>True if range is valid, else false</returns>
Public Function IsValid() As [Boolean]
Return Minimum.CompareTo(Maximum) <= 0
End Function
''' <summary>
''' Determines if the provided value is inside the range
''' </summary>
''' <param name="value">The value to test</param>
''' <returns>True if the value is inside Range, else false</returns>
Public Function ContainsValue(value As T) As [Boolean]
Return (Minimum.CompareTo(value) <= 0) AndAlso (value.CompareTo(Maximum) <= 0)
End Function
''' <summary>
''' Determines if this Range is inside the bounds of another range
''' </summary>
''' <param name="Range">The parent range to test on</param>
''' <returns>True if range is inclusive, else false</returns>
Public Function IsInsideRange(Range As Range(Of T)) As [Boolean]
Return Me.IsValid() AndAlso Range.IsValid() AndAlso Range.ContainsValue(Me.Minimum) AndAlso Range.ContainsValue(Me.Maximum)
End Function
''' <summary>
''' Determines if another range is inside the bounds of this range
''' </summary>
''' <param name="Range">The child range to test</param>
''' <returns>True if range is inside, else false</returns>
Public Function ContainsRange(Range As Range(Of T)) As [Boolean]
Return Me.IsValid() AndAlso Range.IsValid() AndAlso Me.ContainsValue(Range.Minimum) AndAlso Me.ContainsValue(Range.Maximum)
End Function
End Class
守ld最後的回報是'240'而不是'220'? – Curt
@Ivinvin對於每種情況,你的結果增加20? – freebird
@raman一個switch語句在這裏不會改變任何東西。 – James