vb 添加一个文本框,Name属性设置为Tex1

2.设置窗体的标题为班级--学号--姓名,在窗体上添加一个命令按钮,Name属性设置为Cmd1。添加一个文本框,Name属性设置为Tex1。在Cmd1的Click事件中编... 2. 设置窗体的标题为班级--学号--姓名,在窗体上添加一个命令按钮,Name属性设置为Cmd1。添加一个文本框,Name属性设置为Tex1。在Cmd1的Click事件中编写程序,定义一个动态数组,其长度为n,n的值由tex1中输入。产生n个100—200之间的随机数,写入到数组中。调用一个过程,实现对这个数组的排序,并在Cmd1的Click事件中输出排序后的数组。 展开
 我来答
KL1112
2010-06-03 · TA获得超过532个赞
知道小有建树答主
回答量:454
采纳率:0%
帮助的人:625万
展开全部
Private Sub Cmd1_Click()
Dim a() As Integer, n As Integer
Dim minNum As Integer, maxNum As Integer
Dim i As Integer

minNum = 100
maxNum = 200

n = Val(Tex1)

If Not GetRndNum(a(), 1, n, minNum, maxNum) Then Exit Sub
Me.Cls
Print "产生" & CStr(n) & "个100—200之间的随机数"
For i = 1 To n
Print CStr(a(i)); " ";
If i Mod 5 = 0 Then Print
Next i

If Not Paixu(a(), 1, n, False) Then Exit Sub
Print
Print "排序:"
For i = 1 To n
Print CStr(a(i)); " ";
If i Mod 5 = 0 Then Print
Next i

Tex1.SetFocus
End Sub

'随机数
Private Function GetRndNum(a() As Integer, L As Integer, U As Integer, minNum As Integer, maxNum As Integer) As Boolean
On Error GoTo ErrGRN

Dim i As Integer, diata As Integer
ReDim a(L To U)
diata = maxNum - minNum + 1
Randomize
For i = L To U
a(i) = Int(Rnd * diata + minNum)
Next i
GetRndNum = True
ErrGRN:
End Function

'排序
'当p=True时从小到大,p=False时从大到小
Private Function Paixu(a() As Integer, L As Integer, U As Integer, p As Boolean) As Boolean
On Error GoTo ErrPx

Dim i As Integer, j As Integer, k As Integer
Dim T As Integer

For i = L To U - 1
k = i
For j = i + 1 To U
If a(k) < a(j) Xor p Then k = j
Next j
If k <> i Then
T = a(i): a(i) = a(k): a(k) = T
End If
Next i
Paixu = True
ErrPx:
End Function
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式