首先,设置并行阵列。然后循环遍历数组以找到匹配范围。如果匹配,则应用折扣。
请参阅下面的示例代码
<%
Function CalcDiscount(nAmount)
' Set up the arrays
Amin = Array(5000,10000,50000)
Amax = Array(9999,49999,-1)
Adiscount = Array(0.05,0.08,0.10)
' Initialise other variables
nUpper = uBound(Adiscount)
i = 0
bDiscount = false
CalcDiscount = 0
' Loop through the array to find a matching amount range
do until (i > nUpper or bDiscount = true)
If (nAmount >= Amin(i) and (nAmount <= Amax(i) or Amax(i) = -1)) Then
' Apply discount
CalcDiscount = nAmount * Adiscount(i)
bDiscount = true
End If
i = i + 1
loop
End Function
' Run some test cases
TotGST = 1000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 5000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 5500
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 9999
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 10000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
TotGST = 50000
nDiscount=CalcDiscount(TotGST)
response.write("TotGST=" & TotGST & " Discount = " & nDiscount & "<BR>")
%>
输出
TotGST=1000 折扣 = 0
总消费税 = 5000 折扣 = 250
总消费税 = 5500 折扣 = 275
TotGST=9999 折扣 = 499.95
TotGST=10000 折扣 = 800
TotGST=50000 折扣 = 5000