我已經寫了一個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」。我必須將所有數字(正數和轉換數字)一起顯示。如何做呢?
不是辦法用VBScript的數組。 –
@ Ekkehard.Horner我沒有看到使用這種方式的任何錯誤。我的答案只是一個例子。 – ManishChristian
wrt'Dim arr()'cf http://stackoverflow.com/a/28798878/603855; 'ReDim arr(desiredUBound)'是所有需要的。 **瞭解** UBound/Last Index與Size避免了創建具有虛假尾部和像'For i = 0到UBound(arr) - 1'這樣容易出錯的循環頭部的數組。 –