0

如果有人可以请帮助将此 if else 语句转换为并行数组以找出计算折扣。

并行阵列

是用于表示记录数组的数据结构。它为记录的每个字段保留一个单独的同类数组,每个字段具有相同数量的元素。然后,位于每个数组中相同索引处的对象隐含地是单个记录的字段。从一个对象到另一个对象的指针被数组索引替换。这与将每条记录的所有字段一起存储在内存中的常规方法形成对比。
例如,可以声明一个包含 100 个姓名的数组,每个姓名是一个字符串,以及 100 个年龄,每个年龄都是一个整数,将每个姓名与具有相同索引的年龄相关联。

'使用 GST 计算总成本的折扣

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 
4

1 回答 1

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 折扣 = 0
总消费税 = 5000 折扣 = 250
总消费税 = 5500 折扣 = 275
TotGST=9999 折扣 = 499.95
TotGST=10000 折扣 = 800
TotGST=50000 折扣 = 5000
于 2015-08-17T00:26:10.600 回答