10.利用随机函数产生10个两位数,用选择法完成10个随机数据的升序排列。
11.某幼儿园只收2-6岁的小孩,2岁入小班,3-4岁入中班,5-6岁入大班。编写一个程序,输入小孩的年龄,显示输入哪个班。
这两道如何做?谢谢.
參考答案:第一道:(两个文本框text1,text2;一个按钮command1)
Dim rndNum(1 To 10) As Integer '定义数组
Private Sub Command1_Click()
Text1.Text = "" '清除输入区
Text2.Text = "" '清除输出区
Randomize
Dim i As Long
For i = 1 To 10 '填充随机数,并使用Cint强制转换为整数类型
rndNum(i) = CInt(Rnd(1) * 100 + 1) + CInt(Rnd(1) * 10) '产生两位数的随机数
Text1.Text = Text1.Text & rndNum(i) & " "
Next i
Dim j As Long
Dim Min As Integer
Dim Temp As Integer
For i = 1 To 9 '10个数进行9轮比较
Min = i '第一轮比较的时候,假设第i个元素最小
'在数组剩下后面的数字中选择出最小的数(i到10)
For j = i + 1 To 10
If rndNum(j) < rndNum(Min) Then Min = j
Next j
'i到10中的最小的数,与第i个元素进行交换
Temp = rndNum(i)
rndNum(i) = rndNum(Min)
rndNum(Min) = Temp
Next i
'显示排序的结果
Text2.Text = ""
For i = 1 To 10
Text2.Text = Text2.Text & rndNum(i) & " "
Next i
End Sub
第二道:(两个文本框text1,text2;一个按钮command1)
Private Sub Command1_Click()
Dim age As Long, className As String
If IsNumeric(Text1) Then
age = CInt(Text1.Text)
If age >= 2 And age <= 6 Then
If age = 2 Then
className = "小班"
ElseIf age >= 5 And age <= 6 Then
className = "大班"
Else
className = "中班"
End If
'className = IIf(age = 2, "小班", IIf(age >= 5 And age <= 6, "大班", "中班"))'另一种判断方法
Else
className = "输入有误,请输入年龄2~6"
End If
else
className = "请输入2~6的整数"
End If
Text2.Text = ""
Text2.Text = className
End Sub