我想定义一个动态数组a(k),i为数组的元素的个数,i的值用inputbox确定。用for循环 在窗体中print a(k)=k 我的代码如下,但是运行时输入i的值后,窗体什么都没显示出来,不知道怎么回事,请高手改正一下 谢谢!
Private Sub Form_Click()
Dim a()
ReDim a(k)
i = Val(InputBox("输入元素个数"))
i = UBound(a)
For k = 1 To i
a(k) = k
Print a(k);
Next k
End Sub
ä½ redimçæ¶åk没æå¼ï¼èä¸ä½ çiåkç¨ä¹±äºãçæµä½ çç®çæ¯è¾å
¥ä¸ä¸ªæ°åå°kï¼ç¶åéåèµå¼ã
è¿æ ·å两个æ¹å¨ï¼
1. é¤äºReDim a(k) å¤ï¼äº¤æ¢ææçkåiï¼å 为æä¹ æ¯ï¼i ç¨ä½å¾ªç¯åéï¼èä½ æ°å¥½ä¹å¸æç¨kæ¥è¡¨ç¤ºæ°ç»çé¿åº¦ï¼
2. æReDimè¯å¥æ¾å¨InputBoxèµå¼ä¹åï¼å¹¶å æ i = UBound(a)ï¼å 为Redim a(k)ä¹åï¼k就已ç»æ¯UBound(a)äºã
ç»ææ¯
Private sub form_click()
dim a()
k = val(inputbox("è¾å ¥å ç´ ä¸ªæ°"))
redim a(k)
for i = 1 to k
a(i) = i
print a(i);
next i
End Sub
è¿æ ·ï¼è¿ä¸ªç¨åºå·²ç»å¯ä»¥ç¨äºï¼ä½æ¯ä¸é¢æéè¦è¯´ä¸ä¸ç¼ç¨ä¹ æ¯çé®é¢
1. ä½ çåéé½æ²¡æ声æï¼
dim æ¯ç¨æ¥å£°æåéçï¼VBçéä¸¥æ ¼æ¨¡å¼éé¢ï¼å 许ä¸å£°æåéç´æ¥èµå¼ï¼ä½æ¯éçä½ ç¨åºçè¶æ¥è¶å¤æï¼ä¸å£°æåéä¼é æå¾å¤å¥æªçé¾ä»¥æ¾å°çé误ã声æåéçæ¹å¼æ¯ï¼
Dim i as Integer, k as integer, a(k) as integer
è¿æ ·ç声æè½ç¶æ¯è¾éº»ç¦ï¼ä½æ¯è¿ä¸ºäºé¿å å¥æªçé误ï¼ä¸å®è¦å »æè¯å¥½çä¹ æ¯ãä½ å¯ä»¥å¨ä»£ç éçæä¸é¢æå ¥ä¸è¡ï¼ Option Explicit å ä¸è¿ä¸å¥çæææ¯ï¼å¦ææä¸åéå没æ被声æå°±ä¼æ¥é
2. è¿ä¸ªç¨åºè½ç¶è½å¾åºæ£ç¡®çç»æï¼ä½æ¯è¦æ³¨æï¼æ°ç»çé»è®¤LBoundï¼æå°ä¸æ ï¼æ¯0ï¼ä¹å°±æ¯è¯´ï¼ä½ å¦ækå¼è¾å ¥4ï¼å®é ä¸æ°ç»ç»ææ¯0ã1ã2ã3ã4ï¼ä¸å ±5个ï¼åªæ¯a[0]ä½ æ²¡æç¨å°ãå ¶ä»çç¼ç¨è¯è¨ä¹é½æ¯0å¼å§çï¼ä»¥åæ ¹æ®å®é æ åµï¼åºè¯¥ä¼éå°0å¼å¤´çæ°ç»
è¿æ ·å两个æ¹å¨ï¼
1. é¤äºReDim a(k) å¤ï¼äº¤æ¢ææçkåiï¼å 为æä¹ æ¯ï¼i ç¨ä½å¾ªç¯åéï¼èä½ æ°å¥½ä¹å¸æç¨kæ¥è¡¨ç¤ºæ°ç»çé¿åº¦ï¼
2. æReDimè¯å¥æ¾å¨InputBoxèµå¼ä¹åï¼å¹¶å æ i = UBound(a)ï¼å 为Redim a(k)ä¹åï¼k就已ç»æ¯UBound(a)äºã
ç»ææ¯
Private sub form_click()
dim a()
k = val(inputbox("è¾å ¥å ç´ ä¸ªæ°"))
redim a(k)
for i = 1 to k
a(i) = i
print a(i);
next i
End Sub
è¿æ ·ï¼è¿ä¸ªç¨åºå·²ç»å¯ä»¥ç¨äºï¼ä½æ¯ä¸é¢æéè¦è¯´ä¸ä¸ç¼ç¨ä¹ æ¯çé®é¢
1. ä½ çåéé½æ²¡æ声æï¼
dim æ¯ç¨æ¥å£°æåéçï¼VBçéä¸¥æ ¼æ¨¡å¼éé¢ï¼å 许ä¸å£°æåéç´æ¥èµå¼ï¼ä½æ¯éçä½ ç¨åºçè¶æ¥è¶å¤æï¼ä¸å£°æåéä¼é æå¾å¤å¥æªçé¾ä»¥æ¾å°çé误ã声æåéçæ¹å¼æ¯ï¼
Dim i as Integer, k as integer, a(k) as integer
è¿æ ·ç声æè½ç¶æ¯è¾éº»ç¦ï¼ä½æ¯è¿ä¸ºäºé¿å å¥æªçé误ï¼ä¸å®è¦å »æè¯å¥½çä¹ æ¯ãä½ å¯ä»¥å¨ä»£ç éçæä¸é¢æå ¥ä¸è¡ï¼ Option Explicit å ä¸è¿ä¸å¥çæææ¯ï¼å¦ææä¸åéå没æ被声æå°±ä¼æ¥é
2. è¿ä¸ªç¨åºè½ç¶è½å¾åºæ£ç¡®çç»æï¼ä½æ¯è¦æ³¨æï¼æ°ç»çé»è®¤LBoundï¼æå°ä¸æ ï¼æ¯0ï¼ä¹å°±æ¯è¯´ï¼ä½ å¦ækå¼è¾å ¥4ï¼å®é ä¸æ°ç»ç»ææ¯0ã1ã2ã3ã4ï¼ä¸å ±5个ï¼åªæ¯a[0]ä½ æ²¡æç¨å°ãå ¶ä»çç¼ç¨è¯è¨ä¹é½æ¯0å¼å§çï¼ä»¥åæ ¹æ®å®é æ åµï¼åºè¯¥ä¼éå°0å¼å¤´çæ°ç»
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-11-21
Private Sub Form_Click()
Dim a()
i = Val(InputBox("输入元素个数"))
ReDim a(i)
For i = 0 To UBound(a)-1
a(i) = i
Print a(i);
Next i
End Sub
Dim a()
i = Val(InputBox("输入元素个数"))
ReDim a(i)
For i = 0 To UBound(a)-1
a(i) = i
Print a(i);
Next i
End Sub
第2个回答 2011-11-21
Option Base 0
Private Sub Form_Click()
Dim a()
i = Val(InputBox("输入元素个数"))
ReDim a(i - 1)
For k = 0 To UBound(a)
a(k) = k
Print a(k);
Next k
End Sub
Private Sub Form_Click()
Dim a()
i = Val(InputBox("输入元素个数"))
ReDim a(i - 1)
For k = 0 To UBound(a)
a(k) = k
Print a(k);
Next k
End Sub
第3个回答 2022-10-14
变体型variant