2016-10-19 105 views
-1

我需要計算並得到一個具有多個計算條件的單元的總額。如果我在文本框中輸入金額並按Enter鍵,則第二個文本框應顯示以下條件的總金額。 (如果單位= 450) 0-100 = 3.00 // 101-200 = 5.00 // 201-300 = 7.00 //超過300 = 10.00 如果我在輸入單位後按下輸入450秒的文本框應顯示總計爲2000.00。我是新來vb.net任何人都可以幫我做這件事 (對於第一個100 => 300.00 /秒100 => 500.00 /第三100 => 700.00 /第四100 => 500.00乾脆完全2000.00)vb.net多個條件下的計算

+1

請告訴我們你到目前爲止所嘗試過的,我知道你是新人,但你必須嘗試過一些東西。讓我們看看你的代碼 – SilverShotBee

回答

0

這裏一個簡單的控制檯應用程序來說明過程。我們的想法是從最高的範圍到最低的範圍,並且始終檢查我們是否必須從當前範圍獲取項目。

Class AmountLimit 
    Public Property AmountLowerBound As Integer 
    Public Property Value As Double 

    Public Sub New(amountLowerBound As Integer, value As Double) 
     Me.AmountLowerBound = amountLowerBound 
     Me.Value = value 
    End Sub 
End Class 

Sub Main() 
    'Specify the values for each range. The first number is the lower bound for the range 
    Dim amountLimits As New List(Of AmountLimit) 
    amountLimits.Add(New AmountLimit(0, 3)) 
    amountLimits.Add(New AmountLimit(100, 5)) 
    amountLimits.Add(New AmountLimit(200, 7)) 
    amountLimits.Add(New AmountLimit(300, 10)) 

    Console.Write("Enter the total amount: ") 
    Dim totalAmount = Integer.Parse(Console.ReadLine()) 

    Dim finalValue As Double = 0 
    For i As Integer = amountLimits.Count - 1 To 0 Step -1 
     'Check if our total amount is greater than the current lower bound 
     Dim currentRange = amountLimits(i) 
     If (totalAmount > currentRange.AmountLowerBound) Then 
      finalValue += (totalAmount - currentRange.AmountLowerBound) * currentRange.Value 
      totalAmount = currentRange.AmountLowerBound 
     End If 
    Next 
    Console.WriteLine(finalValue) 
End Sub