我需要在我的VBScript中创建一个条件For...Next
循环,它需要有多个(这里我需要五个)子条件,它可以控制循环的行为。
这是我目前的代码:
TOTAL_1 = 1
TOTAL_2 = 2
TOTAL_3 = 3
TOTAL_4 = 4
TOTAL_5 = 5 '<< Those are values of sub "To" conditions
TOTAL = TOTAL_1 + TOTAL_2 + TOTAL_3 + TOTAL_4 + TOTAL_5 '<< Total value of main "To" condition
For I = 1 To TOTAL
If I = 1 Then WScript.Echo "Currently in Set 1" '<< For sub condition TOTAL_1
If I = 2 Then WScript.Echo "Currently in Set 2"
If I = 3 Then WScript.Echo "Currently in Set 2" '<< For sub condition TOTAL_2
If I = 4 Then WScript.Echo "Currently in Set 3"
If I = 5 Then WScript.Echo "Currently in Set 3"
If I = 6 Then WScript.Echo "Currently in Set 3" '<< For sub condition TOTAL_3
If I = 7 Then WScript.Echo "Currently in Set 4"
If I = 8 Then WScript.Echo "Currently in Set 4"
If I = 9 Then WScript.Echo "Currently in Set 4"
If I = 10 Then WScript.Echo "Currently in Set 4" '<< For sub condition TOTAL_4
If I = 11 Then WScript.Echo "Currently in Set 5"
If I = 12 Then WScript.Echo "Currently in Set 5"
If I = 13 Then WScript.Echo "Currently in Set 5"
If I = 14 Then WScript.Echo "Currently in Set 5"
If I = 15 Then WScript.Echo "Currently in Set 5" '<< For sub condition TOTAL_5
Next
虽然上面的代码可以工作,但每次我改变像
这样的子条件的值时,我都需要更改For循环TOTAL_1 = 20, TOTAL_4 = 8
这个For循环应该在变量TOTAL
中执行15次,但是即使I
的值在循环中发生变化,它也应该检查当前I
所属的子条件是什么然后做同样的工作(这里显示相同的消息),直到I
的值属于下一个子条件。
如果我将子条件TOTAL_1
的值更改为5,我需要在此For循环中进行以下更改:
If I = 1 Then WScript.Echo "Currently in Set 1"
If I = 2 Then WScript.Echo "Currently in Set 1"
If I = 3 Then WScript.Echo "Currently in Set 1"
If I = 4 Then WScript.Echo "Currently in Set 1"
If I = 5 Then WScript.Echo "Currently in Set 1" '<< For changed sub condition TOTAL_1
我还需要在将来添加更多子条件,例如TOTAL_6
,TOTAL_7
....
如何在不更改for循环的情况下执行此操作,如何从此代码中删除任意行,使其更小?
答案 0 :(得分:1)
如何修改If条件如下所示:
auth_type: 'reauthenticate'
您可以为TOTAL_6,7添加更多条件,依此类推......
编辑2:为了使它更短,你可以这样做:
For I = 1 To TOTAL
If I <= TOTAL_1 Then
WScript.Echo "Currently in Set 1" '<< For sub condition TOTAL_1
ElseIf I>TOTAL_1 and I<=TOTAL_1+TOTAL_2 Then
WScript.Echo "Currently in Set 2"
ElseIf I>TOTAL_1+TOTAL_2 and I<=TOTAL_1+TOTAL_2+TOTAL_3 Then
WScript.Echo "Currently in Set 3" '<< For sub condition TOTAL_3
ElseIf I>TOTAL_1+TOTAL_2+TOTAL_3 and I<=TOTAL_1+TOTAL_2+TOTAL_3+TOTAL_4 Then
WScript.Echo "Currently in Set 4" '<< For sub condition TOTAL_4
ElseIf I>TOTAL_1+TOTAL_2+TOTAL_3+TOTAL_4 and I<=TOTAL Then
WScript.Echo "Currently in Set 5" '<< For sub condition TOTAL_5
Else
Wscript.Echo "Not in any set"
End If
Next
在第二个解决方案中,您只需要向数组中添加更多元素,不做任何其他更改。