2015-08-26 56 views
0

我已經寫了一個VBScript來找出一個數組正數和負數。我設法分開並打印這些數字。我現在想將正數轉換爲負數。從數組轉換負數正數

這是我寫的VBScript中:

Option explicit 
    Dim arr(), i, str, number,j,k,str1,p 
    number=inputbox("The number of elements the array should have") 
    ReDim arr(number-1) 

    arr(0)=1 
    arr(1)=2 
    arr(2)=-4 
    arr(3)=6 
    arr(4)=-8 


    For i=0 to number-1 
     If arr(i)>0 Then 
      j=i 
      str=str&vbnewline&arr(j)&vbnewline 
      msgbox ("The positive numbers from the array are " &str) 
    end if 
    next 
    For i=0 to number-1 
     If arr(i)<0 Then 
      k=i 
      str1=str1&vbnewline&arr(k)&vbnewline 
      msgbox ("The negative numbers from the array are " &str1) 
     End If 
     Next 

'ReDim preserve arr(6) 
    'For i= 5 to 7 
     'p=arr(k)*(-1) 
     'Next 
    'msgbox p 

腳本成功執行,直到第二個for循環。我試圖將數組中的負數轉換爲正數(在本例中爲-4,-8)。當我執行註釋代碼時,我只能得到第二個數字「8」。我必須將所有數字(正數和轉換數字)一起顯示。如何做呢?

回答

0

你是對的,但是你在最後一個註釋for loop中使用了負數的最後一項,因此你錯過了-4。嘗試是這樣的:

Dim arr() 
ReDim arr(5) 

arr(0)=1 
arr(1)=2 
arr(2)=-4 
arr(3)=6 
arr(4)=-8 

For i = 0 to UBound(arr) - 1 
    If arr(i) < 0 Then 
     Msgbox arr(i) 
     arr(i) = arr(i)*-1 '<-- This will convert all the negative to positive 
    End If 
Next 

For i = 0 to UBound(arr) - 1 
    Msgbox arr(i) 
Next 
+0

不是辦法用VBScript的數組。 –

+0

@ Ekkehard.Horner我沒有看到使用這種方式的任何錯誤。我的答案只是一個例子。 – ManishChristian

+0

wrt'Dim arr()'cf http://stackoverflow.com/a/28798878/603855; 'ReDim arr(desiredUBound)'是所有需要的。 **瞭解** UBound/Last Index與Size避免了創建具有虛假尾部和像'For i = 0到UBound(arr) - 1'這樣容易出錯的循環頭部的數組。 –

-2

這就是動態數組想如何在VBScript中被處理:

Option Explicit 

' Decent way to initialize a dynamic array 
ReDim a(4) ' 5 slots 0 .. 4; no "Dim a()" == spurious creation of an abomination 
a(0) = 1 
a(1) = 2 
a(2) = -4 
a(3) = 6 
a(4) = -8 
WScript.Echo "a = [" & Join(a) & "]" 
Dim b : b = mapFO(a, New cSignSwitch) 
WScript.Echo "b = [" & Join(b) & "]" 
WScript.Echo "neg [" & Join(grepFF(a, GetRef("lessZero"))) & "]" 
WScript.Echo "else [" & Join(grepFF(a, GetRef("geZero"))) & "]" 
' Convenient way to initialize a dynamic array of Longs 
Dim c : c = mapFF(Split("1 2 -4 6 -8"), GetRef("XCLng")) 
WScript.Echo "c = [" & Join(c) & "]" 
mapSE c, "a(i) * a(i)" 
WScript.Echo "x*x [" & Join(c) & "]" 

' return new array from mapping a's elements via o.map 
Function mapFO(a, o) 
    Dim t : t = a ' array assignment copies! 
    mapSO t, o 
    mapFO = t 
End Function 

' return new array from mapping a's elements via f 
Function mapFF(a, f) 
    Dim t : t = a ' array assignment copies! 
    mapSF t, f 
    mapFF = t 
End Function 

' apply o.map to a's elements 
Sub mapSO(a, o) 
    Dim i 
    For i = 0 To UBound(a) 
     a(i) = o.map(a(i)) 
    Next 
End Sub 

' apply f to a's elements 
Sub mapSF(a, f) 
    Dim i 
    For i = 0 To UBound(a) 
     a(i) = f(a(i)) 
    Next 
End Sub 

' eval ev for a's elements 
Sub mapSE(a, ev) 
    Dim i 
    For i = 0 To UBound(a) 
     a(i) = Eval(ev) 
    Next 
End Sub 

Class cSignSwitch 
    Function map(e) 
    map = e * -1 
    End Function 
End Class 

' return new array of a's elements satisfying f 
Function grepFF(a, f) 
    ReDim t(UBound(a)) ' result can't be larger than source 
    Dim j : j = -1  ' assume empty result 
    Dim i 
    For i = 0 To UBound(a) 
     If f(a(i)) Then 
     j = j + 1 
     t(j) = a(i) 
     End If 
    Next 
    ReDim Preserve t(j) 
    grepFF = t 
End Function 

Function lessZero(n) 
    lessZero = (n < 0) 
End Function 

Function geZero(n) 
    geZero = Not lessZero(n) 
End Function 

Function XCLng(x) 
    XCLng = CLng(x) 
End Function 

輸出:

cscript 32233658.vbs 
a = [1 2 -4 6 -8] 
b = [-1 -2 4 -6 8] 
neg [-4 -8] 
else [1 2 6] 
c = [1 2 -4 6 -8] 
x*x [1 4 16 36 64] 
+0

這不是問題的答案。 – ManishChristian