当前位置:编程学习 > VB >>

这个VB程序具体代码怎么写?

随机10个数。按从小到大和从大到小的顺序排列。

求具体编码。复制至VB内有效给分!

答案:Dim a(1 To 10)
Private Sub Command1_Click()
    For i = 1 To 9
        For j = i + 1 To 10
            If a(i) > a(j) Then
                b = a(i)
                a(i) = a(j)
                a(j) = b
            End If
        Next j
    Next i
    Label2.Caption = "排序后的数为:"
    For k = 1 To 10
        Label2.Caption = Label2.Caption & a(k) & "  "
    Next k
End Sub

Private Sub Form_Load()

    Randomize
    Label1.Caption = "产生的随机数为:"
    For i = 1 To 10
        a(i) = Int(Rnd * 99 + 1) '我这里是产生1到100的整数,按你需求更改99是上限减1,1是下限
        Label1.Caption = Label1.Caption & a(i) & "  "
    Next i
End Sub

我已经尽最大的努力写了一个经典的冒泡法程序并加以详细的注释供你参考希望你能看得明白其中的冒泡原理,详见下述:

所需控件:CommandButton,Textbox.

注意Text的Multiline属性应设为True,即可接受换行

dim a(1 to 10)
for i=1 to 10
a(i)=rnd
next i
for i=1 to 9
for j=i+1 to 10
if a(i)<a(j) then b=a(i):a(i)=a(j):a(j)=b'如果要从小到大排列,小于号改为大于号
next j,i
for i=1 to 10
print a(i);
next i

冒泡法:

option base 1

dim c(10) as integer '要排几个数就输入几

randomize

for i = 1 to ubound(c)

c(i)=int(rnd*100)+1 '想要多大范围就输入在*后,想从那个数字开始就输入+后

next i

for i = 1 to ubound(c)

for j = i+1 to ubound(c)

if c(i)>c(j) then exchange(a(i),a(j) '同那个人说的,从大到小是<号

next j

next i

for i = 1 to ubound(c)

print a(i);

next i

 

function exchange(x as integer,y as integer)

dim z as integer

z=y

y=x

x=z

end function

private sub form1_click() cls dim i,k,a(1 to 10),e   for i=1 to 10  a(i)=rnd print a(i); next i print for i=1 to 9 for k=i+1 to 10 if a(i)<a(k) then e=a(i);a(i)=a(k);a(k)=e print a(k); next k next i show  end sub
对于排序来说,有几种算法可以参考:快速、选择和冒泡排序

'添加两个command  和一个label 控件

Private Sub Command1_Click()
Dim a(1 To 10)
For i = 1 To 10
a(i) = Int(100 * Rnd + 1)
Next i
For i = 1 To 9
For j = i + 1 To 10
If a(i) < a(j) Then
b = a(i): a(i) = a(j): a(j) = b
End If
Next j, i
For i = 1 To 10
Label1.Caption = a(i) & "  " & Label1.Caption

Next i
End Sub

Private Sub Command2_Click()
Dim a(1 To 10)
For i = 1 To 10
a(i) = Int(100 * Rnd + 1)
Next i
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
b = a(j): a(j) = a(i): a(i) = b
End If
Next j, i
For i = 1 To 10
Label1.Caption = a(i) & "  " & Label1.Caption
Next i
End Sub

Private Sub Form_Load()
Command1.Caption = "小→大"
Command2.Caption = "大→小"

End Sub

上一个:VB怎么做成电脑属性那样翻页
下一个:关于VB问题,希望高手帮解决

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,