|
本帖最后由 cashiba 于 2017-4-7 21:41 编辑 local $arr=['12','1','2','3','4','5','6','7','8','9','10','11','12']
local $brr=['15','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
Switch @Month
Case 1
$sMsg = "a"
Case 2
$sMsg = "b"
Case 3
$sMsg = "c"
Case 4
$sMsg = "d"
Case 5
$sMsg = "e"
Case 6
$sMsg = "f"
Case 7
$sMsg = "g"
Case 8
$sMsg = "h"
Case 9
$sMsg = "i"
Case 10
$sMsg = "j"
Case 11
$sMsg = "k"
Case 12
$sMsg = "l"
EndSwitch
如上,条件选择语句,与两个数组对应。
假如数组元素比较多,达数百上千个的话,switch语句将很长。
用如下循环方式local $arr=['12','1','2','3','4','5','6','7','8','9','10','11','12']
local $brr=['15','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
Switch $arr
for $i=1 to $arr[0]
Case $arr[$i]
$sMsg = $brr[$i]
next
EndSwitch
书写代码的话语法上又不可行。
改成如下方式一一赋值的话,每选择一次都要遍历一次数组local $arr=['12','1','2','3','4','5','6','7','8','9','10','11','12']
local $brr=['15','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
for $i=1 to $arr[0]
if $m=$arr[$i] then $sMsg = $brr[$i]
next
除了资源浪费,在逻辑上又觉得跟switch语句不一样。
最终目的
减少书写代码时重复复制粘贴劳动是一方面
另一方面是能精简代码,使之更直观,效率更高。 |
|