-8

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

我正在寻找一个合适的公式。

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

4 回答 4

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);
于 2012-10-10T10:54:57.050 回答
0

我不能让它比以下更短:

Return ((number \ 1000) + 1) * 20
于 2012-10-10T10:27:24.807 回答
0

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

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

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

所以:

Return (Math.Ceiling(number/1000)*1000) / 50
于 2012-10-10T10:37:12.470 回答
0

您可以使用自定义Range类和 aDicitonary来存储范围+值:

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
于 2012-10-10T10:53:43.820 回答