从数组中获取随机项
Get a Random Item from an Array
我从这个基本代码示例开始:
<%
block1 = "tree, ball, cheese, rabbit, waffle, planet, string, cat, dog, hole, hobbit, sing,"
wordArray1 = split(block1, ",")
For Each item In wordArray1
Response.Write(item & "<br />")
Next
max=11
min = 1
Randomize
rand1 = Int((max-min+1)*Rnd+min)
response.write "<hr/>rand1: " & rand1 & "<hr/>"
w1 = wordArray1(0,rand1)
response.write "w1: " & w1 & "<hr/>"
%>
我从一个用逗号分隔的单词列表开始 (block1)。
我将其转换为数组 (wordArray1)
我可以通过遍历数组并将数组值打印到页面来证明数组存在。
然后我希望能够从该数组中随机 select 仅 1 个值。
我以为我可以做类似的事情:
max=11
min = 1
Randomize
rand1 = Int((max-min+1)*Rnd+min)
response.write "<hr/>rand1: " & rand1 & "<hr/>"
w1 = wordArray1(0,rand1)
但是我得到这个错误:
Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'wordArray1'
如何随机访问数组之一?
谢谢
您没有创建多维数组,因此不应传入两个参数。更改此行:
w1 = wordArray1(0,rand1)
对此:
w1 = wordArray1(rand1)
我从这个基本代码示例开始:
<%
block1 = "tree, ball, cheese, rabbit, waffle, planet, string, cat, dog, hole, hobbit, sing,"
wordArray1 = split(block1, ",")
For Each item In wordArray1
Response.Write(item & "<br />")
Next
max=11
min = 1
Randomize
rand1 = Int((max-min+1)*Rnd+min)
response.write "<hr/>rand1: " & rand1 & "<hr/>"
w1 = wordArray1(0,rand1)
response.write "w1: " & w1 & "<hr/>"
%>
我从一个用逗号分隔的单词列表开始 (block1)。
我将其转换为数组 (wordArray1)
我可以通过遍历数组并将数组值打印到页面来证明数组存在。
然后我希望能够从该数组中随机 select 仅 1 个值。
我以为我可以做类似的事情:
max=11
min = 1
Randomize
rand1 = Int((max-min+1)*Rnd+min)
response.write "<hr/>rand1: " & rand1 & "<hr/>"
w1 = wordArray1(0,rand1)
但是我得到这个错误:
Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'wordArray1'
如何随机访问数组之一?
谢谢
您没有创建多维数组,因此不应传入两个参数。更改此行:
w1 = wordArray1(0,rand1)
对此:
w1 = wordArray1(rand1)