2016-08-31 44 views
-1

我试图从1到150打印范围从1到150以及一些文本。 即第一个范围是从升序1到150,第二个是从150降到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应该减少。有没有办法在同一个循环中添加这个n?

+0

代替仅仅使用我的,使用150 - 我在第二个TL1() –

+3

@罗伯特阿尔特曼,实际上它应该是'151 - 我',因为'我'从1开始 –

+0

@VictorMorae s的touche先生。接得好! –

回答

1

只需要计算减少增量值所需的总和,正如评论中所讨论的那样。还要注意使用Const来定义常量值,这样当值更改时,您不必经过调整数值的代码行。

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 

输出:

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; 
0

而不是只利用I,使用151 - i。在第二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