| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1100 人关注过本帖, 1 人收藏
标题:我想用冒泡法把9个数从小到大排序,求好心人帮我看下下面这段小程序显示结果 ...
只看楼主 加入收藏
btt6
Rank: 2
等 级:论坛游民
帖 子:46
专家分:10
注 册:2016-8-13
结帖率:47.83%
收藏(1)
 问题点数:0 回复次数:2 
我想用冒泡法把9个数从小到大排序,求好心人帮我看下下面这段小程序显示结果为何不正确
我想用冒泡法把9个数从小到大排序,求好心人帮我看下下面这段小程序显示结果为何不正确
Private Sub Command1_Click()
Dim a(1 To 9) As Integer
Dim i As Integer, h As Integer
For i = 1 To 9
   a(i) = InputBox("任意输入一位整数")
Next i
i = 1
h = 0
While i < 9
   If a(i) > a(i + 1) Then
      h = a(i + 1)
      a(i + 1) = a(i)
      a(i) = h
   End If
   i = i + 1
Wend
i = 1
Do
   Print a(i);
   i = i + 1
Loop Until i > 9
End Sub
2016-09-13 16:17
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
你的泡泡冒了几轮??
有10个数,最多需要冒9轮,你只有一轮。结果怎么会正确呢。

百度百科里的一个例子,很有参考价值。
程序代码:
Sub maopao()
    Dim a = Array(233, 10086, 31, 15, 213, 5201314, 427)
    Dim i As Integer, j As Integer
     
    For i = UBound(a) - 1 To 0 Step -1
        For j = 0 To i
            If a(j) > a(j + 1) Then
                a(j) = a(j) + a(j + 1)
                a(j + 1) = a(j) - a(j + 1)
                a(j) = a(j) - a(j + 1)
            End If
        Next j
    Next i
    For i = 0 To UBound(a)
        Print a(i)
    Next i
End Sub

授人于鱼,不如授人于渔
早已停用QQ了
2016-09-13 18:55
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:0 
其实排序是非常简单的,看下面的例子
程序代码:
Private Sub Command1_Click()
Me.Show
Me.Cls
Dim a(1 To 9) As Integer
Dim I As Integer, J As Integer, XX As Integer
For I = 1 To 9
   Randomize
   a(I) = Int(Rnd * 90 + 10)
Next I
For I = 1 To 8
    For J = I + 1 To 9
        If a(I) > a(J) Then
            XX = a(I)
            a(I) = a(J)
            a(J) = XX
        End If
    Next J
Next I
For I = 1 To 9
    Print a(I);
Next I
End Sub

请不要选我!!!
2016-09-14 13:41
快速回复:我想用冒泡法把9个数从小到大排序,求好心人帮我看下下面这段小程序显 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039646 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved