VB编程,高手帮编一下

来源:百度知道 编辑:UC知道 时间:2024/06/03 02:31:32
在一个从小到大排序的有序数组中插入一个新的数字,并使原数组依然保持有序。例如,在数组data中原有的数据为“3,6,9,12,15,18”,从键盘输入数字“7”后,数据变为“3,6,7,9,12,15,18”

dim a() as integer,n%,k%
a=array(3,6,9,12,15,18)
n=ubound(a)
k=val(inputbox("please input"))
for i= 0 to n
if a(i)>k then exit for
next i
redim a(n+1)
for j= n= i step -1
a(j+1)=a(j)
next j
a(i)=k
for i=0 to n+1
print a(i)
next i

Dim a
Dim b(6) As Integer
Private Sub Command1_Click()
c = Int(Text1.Text)
For i = 0 To 5
If a(i) > c Then
Exit For
End If
Next i
For j = 0 To 6
If j < i Then
b(j) = a(j)
ElseIf j = i Then
b(j) = c
Else
b(j) = a(j - 1)
End If
Next j
For i = 0 To 6
Print b(i);
Next i
End Sub

Private Sub Form_Load()
a = Array(3, 6, 9, 12, 15, 18)
End Sub