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

VB问题的解答~

这是一个关于数组使用的练习,原本在点击排序后label2应该显示之前随机生成数从小到大的顺序,可是现在点击后出现的值都是一模一样的,麻烦各位大侠帮小弟检查一下是哪里出了问题,小弟感激不尽哪~

补充:Option Base 1 '指定数组的下标下界为1
Dim a(7) As Integer  '定义数组 a(1 to 7)为整形


Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Form_Load
Case 1
For i = 1 To 6 '第一个循环i,初值为1,终值为6,步长为1
For j = i + 1 To 7 '嵌套第二个循环j,步长为1
If a(i) > a(j) Then '如果数组a的元素a(i)>a(j)
t = a(i): a(i) = a(j): a(j) = t '进行选择交换
End If
Next '结束第二个循环
Next '结束第一个循环

p = Str(a(1)) '将数组元素a(1)的值转化为整形后赋给p
For i = 2 To 7 '循环变量i,初值为2,终值为7,步长为1
p = p & "," & Str(a(1)) 'p显示的形式为 p,a(1)的整形值
Next
Label2.Caption = LTrim(p) 'label2显示时去掉p左边的空格
Case 2
Unload Me '结束程序
End Select

End Sub

Private Sub Form_Load()
Dim p As String '定义p为字符型
Randomize '避免生成的值重复
p = ""
For i = 1 To 7 '循环变量i,初值为1,终值为7,步长为1
Do
x = Int((34 + 1 - 1) * Rnd + 1) 'x的值为1到34内随机生成的整数
yes = 0
For j = 1 To i - 1 '循环变量j,初值为1,终值为i-1,步长为1
If x = a(j) Then yes = 1: Exit For '若x=a(j)那么yes的值为1;结束if
Next
Loop While yes = 1
a(i) = x
p = p & Str(a(i)) & "," 'p显示的形式为 p,a(1)的整形值
Next
Label1.Caption = LTrim(Left(p, Len(p) - 1)) '
Label2.Caption = ""


End Sub

答案:Option Base 1 '指定数组的下标下界为1
Dim a(7) As Integer  '定义数组 a(1 to 7)为整形


Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Form_Load
Case 1
For i = 1 To 6 '第一个循环i,初值为1,终值为6,步长为1
For j = i + 1 To 7 '嵌套第二个循环j,步长为1
If a(i) > a(j) Then '如果数组a的元素a(i)>a(j)
t = a(i): a(i) = a(j): a(j) = t '进行选择交换
End If
Next '结束第二个循环
Next '结束第一个循环

p = Str(a(1)) '将数组元素a(1)的值转化为整形后赋给p
For i = 2 To 7 '循环变量i,初值为2,终值为7,步长为1
p = p & "," & Str(a(i)) 'p显示的形式为 p,a(1)的整形值
Next
Label2.Caption = LTrim(p) 'label2显示时去掉p左边的空格
Case 2
Unload Me '结束程序
End Select

End Sub

Private Sub Form_Load()
Dim p As String '定义p为字符型
Randomize '避免生成的值重复
p = ""
For i = 1 To 7 '循环变量i,初值为1,终值为7,步长为1
Do
x = Int((34 + 1 - 1) * Rnd + 1) 'x的值为1到34内随机生成的整数
yes = 0
For j = 1 To i - 1 '循环变量j,初值为1,终值为i-1,步长为1
If x = a(j) Then yes = 1: Exit For '若x=a(j)那么yes的值为1;结束if
Next
Loop While yes = 1
a(i) = x
p = p & Str(a(i)) & "," 'p显示的形式为 p,a(1)的整形值
Next
Label1.Caption = LTrim(Left(p, Len(p) - 1)) '
Label2.Caption = ""


End Sub


 


http://qun.qq.com/air/#228345204/share你懂的,奶奶的改好了回答不了:您回答的内容与别人回答的内容太相似了

上一个:VB编程 代码使用
下一个:VB是什么语言

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,