2017-06-12 49 views
0

我需要在VBScript中创建条件For...Next循环,它需要有多个(在这里我需要五个)子条件,它可以控制循环的行为。VBScript中的条件循环

这里是我当前的代码:

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 

虽然上面的代码工作,我需要我每次改变的子条件的值,如时间更改为循环

TOTAL_1 = 20, TOTAL_4 = 8 

for循环应该执行15在变量TOTAL中分配时间,但在循环中更改值为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_6TOTAL_7 ...在未来。

如何在不更改每次循环的情况下执行此操作,以及如何从此代码删除任意行,从而使其更小?

+1

签入'select case',因为它可能有助于缩短代码。请参阅此链接底部附近的示例:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement –

+0

@PaulT。这是非常有用的,不用介意在VBScript中存在循环。 – GTAVLover

回答

1

如何如下图所示有关修改如果条件:

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 

您可以TOTAL_6,7进一步增加多个条件等等...

编辑2:为了使它更短,你可以做这样的事情:

arr=Array(1,2,3,4,5)     'this array contains all your Total_1,2,3,4,5 values. You can add more. 
fullSum = func_sum(arr,UBound(arr))(1) 'In this case, the value is 15(sum of all elements)  
For i=0 To UBound(arr) 
    sum = func_sum(arr,i) 
    For k=1 To fullSum 
     If k>sum(0) And k<=sum(1) Then 
      WScript.Echo "Currently in Set "&i+1 
     End If 
    Next 
Next 


Function func_sum(intArr, tempPos)  'returns the sums of all elements upto indices tempPos-1 and tempPos 
    tempPos2=tempPos-1 
    sum1=0 
    sum2=0 
    If tempPos=0 Then 
     sum2=intArr(tempPos) 
    Else 
     For j=0 To tempPos2 
      sum1 = sum1 + arr(j) 
     Next 
     sum2 = sum1 + arr(tempPos) 
    End If 
    arrSum = Array(sum1,sum2) 
    func_sum=arrSum 
End Function 

在第二个解决方案,您只需要更多的元素添加到阵列和其他改变什么。

+0

我是多么不幸......我忘记了双重调节....: - | – GTAVLover