在VB.Net中寻找完美的逻辑

时间:2012-10-10 10:24:13

标签: asp.net vb.net

我有这样的情况,如果数字小于1000,则20出现                                                    1001-2000,40来了                                                   2001-3000,66出现在另一个文本框中

我正在寻找一个合适的配方。

If number <= 1000 Then
    Return 20
ElseIf number <= 2000 Then
    Return 40
ElseIf number <= 3000 Then
    Return 60
ElseIf number <= 4000 Then
    Return 80
ElseIf number <= 5000 Then
    Return 100
ElseIf number <= 6000 Then
    Return 120
ElseIf number <= 7000 Then
    Return 140
ElseIf number <= 8000 Then
    Return 160
ElseIf number <= 9000 Then
    Return 180
ElseIf number <= 10000 Then
    Return 200
ElseIf number <= 11000 Then
    Return 220
ElseIf number <= 12000 Then
    Return 240
ElseIf number <= 13000 Then
    Return 260
ElseIf number <= 14000 Then
    Return 280
ElseIf number <= 15000 Then
    Return 300
End If

4 个答案:

答案 0 :(得分:1)

这是你的答案:

<强> VB.Net:

If (number Mod 1000 = 0)
    result = 20 * (number / 1000)
Else
    result = 20 * (number / 1000 + 1)

<强> C#:

if (number % 1000 == 0)
    result = 20 * (number / 1000);
else
    result = 20 * (number / 1000 + 1);

答案 1 :(得分:0)

我不能让它短于:

Return ((number \ 1000) + 1) * 20

答案 2 :(得分:0)

首先,您需要将数字四舍五入到最接近的千位数,以便我们得到一致的结果。

(Math.Ceiling(number/1000)*1000)

然后假设你的意思是每次递增20(我看到评论指的是不正确的值),你可以除以50。

因此:

Return (Math.Ceiling(number/1000)*1000) / 50

答案 3 :(得分:0)

您可以使用自定义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