【问题标题】:Conditional For Loops in VBScriptVBScript 中的条件 For 循环
【发布时间】:2017-06-12 05:06:47
【问题描述】:

我需要在我的 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 Loop,例如

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...。

如何在不每次都更改 for 循环的情况下做到这一点,以及如何从该代码中删除任意行,使其更小?

【问题讨论】:

标签: loops for-loop vbscript


【解决方案1】:

如何修改If条件如下所示:

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

在第二种解决方案中,您只需要向数组中添加更多元素即可。

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 2016-04-10
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2018-06-26
    相关资源
    最近更新 更多