我需要计算并获得具有多个计算条件的单位的总金额。如果我在文本框中输入金额并按Enter键,则第二个文本框应显示以下条件的总金额。 (如果单位= 450) 0-100 = 3.00 // 101-200 = 5.00 // 201-300 = 7.00 //超过300 = 10.00 如果我输入单位后按Enter键为450秒文本框应显示总计为2000.00。我是vb.net的新手,任何人都可以帮我这样做 (对于前100 => 300.00 /秒100 => 500.00 /第三100 => 700.00 /第四100 =>> 500.00总共2000.00)
答案 0 :(得分: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