2014-02-25 85 views
0

我正在創建一個頁面,顯示來自15個不同贊助商的每個頁面加載的4個贊助商廣告。根據贊助商的價值,應該根據廣告的頻率對廣告進行加權。我已經取消了類似的PHP,但似乎無法得到它在ASP在給定關鍵值的Visual Basic數組中找到值

這是我如何去做: 1.創建一個贊助商數組,其中一個「列」表示值贊助 2.總結贊助數組的價值 3.使用總數爲陣列中的每個贊助商創建一個權重百分比,並將此百分比作爲陣列中的附加「列」添加 4.創建一個新的陣列,根據它們的重量重複贊助商值

在ASP中,我最終創建了一個3維數組,它根據重量重複我的初始Sponsors數組數組。它似乎工作 - 我可以寫我的100行newSponsors數組迭代數組的值。

newSponsors(N,X,I)=贊助商(X,I)

然後我隨機4個數字選自100創建用於我的3維陣列的密鑰。我停留在如何將它們進行比較:

SponsorWeight = Sponsors(5,i) 

Dim newSponsors(100,5,15) 

for i = 0 to SponsorTotalCt 
SponsorWeight = Sponsors(5,i) 
for w = 1 to SponsorWeight 
    for x = 0 to 5 
     newSponsors(n,x,i) = Sponsors(x,i) 
     value = Sponsors(x,i) 
     response.write("Value = " & value & "<br>") 
    next 
    n = n + 1 
next 
x = 0 
next 

所以說,我的隨機數發生器產生4張隨機數出來的100:17,9,88和27我需要創建一個if語句:

if newSponsors(17,2,i) = newSponsors(9,2,i) 
    pass = false 

但我不知道我的價值會是什麼。我如何得到它?

回答

0

好,花了一天半的時間,但我終於想出了這個工作代碼 - 可能不是最有效的方法,但它的工作:

i = 0 
For i = 0 to UBound(Sponsors, 2) 
SponsorVal = Sponsors (2,i) 
SponsorWeight = Round(SponsorVal/SponsorTotalAmt * 100,0) 
Sponsors (5,i) = SponsorWeight 

Next 

'CREATE NEW ARRAY OF 100 TO RANDOMIZE AND WEIGHT 
' n = key in 100 array 
' x = key in Sponsors array 
' i = row in Sponsors array 
' w = weight in Sponsor array 

Dim n 
Dim x 

n = 0 
x = 0 
i = 0 
w = 1 
SponsorWeight = Sponsors(5,i) 

Dim newSponsors(100,15) 

for i = 0 to SponsorTotalCt 
SponsorWeight = Sponsors(5,i) 
for w = 1 to SponsorWeight 
    value = i 
    newSponsors(n,0) = value 
    response.write(value) 
    n = n + 1 
next 
x = 0 
next 


' RANDOMIZE A VALUE FROM newSponsors 

intHighNumber = n 
intLowNumber = 1 

pass = "false" 

Dim spot(4) 

For i = 1 to 4 
    Randomize 
    spot(i)= Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber) 
Next 

spot1 = spot(1) 
spot2 = spot(2) 
spot3 = spot(3) 
spot4 = spot(4) 

Sponsor1 = newSponsors(spot1,0) 
Sponsor2 = newSponsors(spot2,0) 
Sponsor3 = newSponsors(spot3,0) 
Sponsor4 = newSponsors(spot4,0) 

'MAKE SURE THAT THERE ARE NO DUPLICATE SPONSORS IN THE 4 RANDOMLY SELECTED 

do 

如果newSponsors(spot1,0 )<>新贊助商(spot2,0)和新贊助商(spot2,0)<> new贊助商(spot1,0) spot3,0)AND new贊助商(spot2,0)<>新贊助商(spot4,0)和新贊助商(spot3,0)<> newSponsors(spot4,0)then pass =「true」 else 通= 「假」 對於i = 1至4 隨機化 點(ⅰ)= INT((intHighNumber - intLowNumber + 1)*了Rnd + intLowNumber) spot1 =點(1) spot2 =點(2) spot3 =點(3) SPOT4 =點(4) Sponsor1 = newSponsors(spot1,0) Sponsor2 = newSponsors(spot2,0) Sponsor3 = newSponsors(spot3,0) Sponsor4 = newSponsors(spot4,0) Next end if Loop Until pass =「true」

response.write("Pass: " & pass & "<br>") 
Response.write(Sponsor1 & "<br>" & Sponsor2 & "<br>" & Sponsor3 & "<br>" & Sponsor4) 

'CALL THE APPROPRIATE VALUE IN THE SPONSORS ARRAY BASED ON THE NEWSPONSORS ARRAY 
Response.write("<br><br>" & Sponsors(Sponsor1,4)) 

希望有人認爲它有幫助!