使用循环增加和减少数量

时间:2016-08-31 14:49:25

标签: for-loop vbscript increment decrement

我试图从1到150打印2个数字的范围以及一些文字。 即第1范围是从1到150的升序,第二范围是从150减1到1。 我可以打印升序,但我不确定如何按降序排列。

Set xml = CreateObject("Microsoft.XMLHTTP")


For i = 1 to 150

 TL1( "HELLO"&i&"INCRESING"&i&";" ) // In both the statement i should increment from 1 to 150 and  
TL1( "HELLO"&n&"INCRESING_I"&i&"DECRESING_N"&n&";" ) //  n should decrement from 150 to 1

Next

Set xml = Nothing

Function TL1( cmd ) 
xml.Open "GET", "http://127.0.0.1:2024/TL1?"&cmd, False
xml.Send
TL1 = xml.responseText
WScript.Stdout.Write cmd & vbCrLf
End Function

所以基本上是在for语句之后。我应该增加,n应该减少。有没有办法可以在同一个forloop中添加这个n?

2 个答案:

答案 0 :(得分:1)

Just need to calculate the sum needed to decrease the increment value, as was being discussed in the comments. Also note the use of Const to define constant values , that way when the values change you don't have to go through lines of code adjusting the numeric values.

Const MIN_LOOP = 1
Const MAX_LOOP = 10

Dim i, n

For i = MIN_LOOP To MAX_LOOP
  WScript.Echo "HELLO" & i & "INCRESING" & i & ";"
  n = (MAX_LOOP + 1) - i
  WScript.Echo "HELLO" & n & "INCRESING_I" & i & "DECRESING_N" & n & ";"
Next

Output:

HELLO1INCRESING1;
HELLO10INCRESING_I1DECRESING_N10;
HELLO2INCRESING2;
HELLO9INCRESING_I2DECRESING_N9;
HELLO3INCRESING3;
HELLO8INCRESING_I3DECRESING_N8;
HELLO4INCRESING4;
HELLO7INCRESING_I4DECRESING_N7;
HELLO5INCRESING5;
HELLO6INCRESING_I5DECRESING_N6;
HELLO6INCRESING6;
HELLO5INCRESING_I6DECRESING_N5;
HELLO7INCRESING7;
HELLO4INCRESING_I7DECRESING_N4;
HELLO8INCRESING8;
HELLO3INCRESING_I8DECRESING_N3;
HELLO9INCRESING9;
HELLO2INCRESING_I9DECRESING_N2;
HELLO10INCRESING10;
HELLO1INCRESING_I10DECRESING_N1;

答案 1 :(得分:0)

Instead of just using i, use 151 - i in the second TL1()

Const MIN_LOOP = 1
Const MAX_LOOP = 10

Dim i, n

For i = MIN_LOOP To MAX_LOOP
    WScript.Echo "HELLO" & i & "INCRESING" & i & ";"
    n = (MAX_LOOP + 1) - i
    WScript.Echo "HELLO" & n & "INCRESING_I" & 151 - i & "DECRESING_N" & n & ";"
Next