如果有人可以帮忙将这个if if语句转换为并行数组,以找出计算折扣。
是用于表示记录数组的数据结构。它为记录的每个字段保留一个单独的同类数组,每个字段具有相同数量的元素。然后,位于每个数组中相同索引处的对象隐含地是单个记录的字段。从一个对象到另一个对象的指针被数组索引替换。这与将每条记录的所有字段存储在存储器中的常规方法形成对比 例如,可以声明一个包含100个名称的数组,每个名称为一个字符串,以及100个年龄,每个年龄都是一个整数,将每个名称与具有相同索引的年龄相关联。
使用商品及服务税的总成本折扣计算
if TotGST >= 5000 AND TotGST <= 9999 then
discount = (TotGST * 0.05)
else
if TotGST >= 10000 AND TotGST <= 49999 then
discount = (TotGST * 0.08)
else
if TotGST >= 50000 then
else
discount = (TotGST * 0.1)
end if
end if
end if
答案 0 :(得分:2)
首先,设置并行数组。 然后循环遍历数组以找到匹配范围。 如果匹配,请应用折扣。
参见下面的示例代码
<%
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 Discount = 0 TotGST=5000 Discount = 250 TotGST=5500 Discount = 275 TotGST=9999 Discount = 499.95 TotGST=10000 Discount = 800 TotGST=50000 Discount = 5000