2017-06-12 3 views
0

Ich muss eine bedingte For...Next Schleife in meinem VBScript erstellen und es muss mehrere (Hier brauche ich fünf) Unterbedingungen in es, die Verhalten der Schleife steuern können.Bedingt für Schleifen in VBScript

Hier ist meine aktuellen Code:

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 

Obwohl obige Code funktioniert, muss ich jedesmal, wenn ich Werte von Unter Bedingungen For-Schleife ändern wie

TOTAL_1 = 20, TOTAL_4 = 8 

Diese For-Schleife ändern sollte 15 ausführen Zeiten wie in Variable TOTAL zugewiesen, aber gerade Wert von I ändert sich in der Schleife, sollte es überprüfen, was die Sub-Bedingung derzeit I gehört und dann die gleiche Arbeit (hier eine gleiche Nachricht anzeigen) bis Wert von I gehört zur nächsten Unterbedingung.

Wenn ich Wert von Unter Zustand ändern TOTAL_1 als 5, ich brauche in diesem For-Schleife folgende Änderung vornehmen:

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 

Ich brauche auch mehr Unter Bedingungen wie TOTAL_6, TOTAL_7 hinzufügen ... in Zukunft.

Wie kann ich dies tun, ohne jedes Mal die for-Schleife zu ändern, und wie kann ich willkürliche Zeilen aus diesem Code entfernen, wodurch dieser kleiner wird?

+1

Klicken Sie auf "Fall auswählen", da dies den Code für Sie verkürzen kann. Siehe das Beispiel am Ende dieses Links: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement –

+0

@PaulT. Das ist sehr nützlich, egal, dass diese Schleife in VBScript existiert. – GTAVLover

Antwort

1

Wie über die Wenn die Bedingungen zu modifizieren, wie unten dargestellt:

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 

Sie können weitere für TOTAL_6,7 weitere Bedingungen hinzufügen und so weiter ...

Edit 2: Um es zu machen noch kürzer, Sie können etwas tun:

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 

In der zweiten Lösung, die Sie brauchen nur mehr Elemente zum Array hinzuzufügen, und sonst nichts zu ändern.

+0

Wie unglücklich bin ich .......... habe ich auch Doppelkonditionierung vergessen ....: - | – GTAVLover