早教吧 育儿知识 作业答案 考试题库 百科 知识分享

用函数产生10个随机数并保存a数组中单击查找任意输入一个数在数组a中查找该数OptionBase1Dima(10)PrivateSubCommand1Click()Text1.Text="":Text2.Text=""Fori=1To10a(i)=Fix(Rnd*99+1)Forj=1Toi-1If

题目详情
用函数产生10个随机数 并保存a数组中 单击查找 任意输入一个数 在数组a中查找该数
Option Base 1
Dim a(10)
Private Sub Command1_Click()
Text1.Text = "":Text2.Text = ""
For i = 1 To 10
a(i) = Fix(Rnd * 99 + 1)
For j = 1 To i - 1
If a(i) = a(j) Then
i = i - 1
Exit For
End If
Next j
Next i
For i = 1 To 10
Text1.Text = Text1.Text + Str(a(i)) + Space(2)
Next i
End Sub
Private Sub Command2_Click()
Dim num As Integer,i As Integer
num = InputBox("请输入查找的数")
For i = 1 To 10
If a(i) = num Then
Text2.Text = Str(num) + "是数组中的第" + Str(i) + "个值"
Exit For
End If
Next i
If i > 10 Then
Text2.Text = Str(num) + "不存于数组中"
End If
End Sub
其中的j循环是什么用处
▼优质解答
答案和解析
Dim x() As Integer,n As Integer,a As Integer,b As Integer,i As Integer
Private Sub sum()
For i = 1 To n
If x(i) Mod 2 = 0 Then
b = b + x(i)
Else
a = a + x(i)
End If
Next i
End Sub
Private Sub Command1_Click()
n = Val(InputBox("输入整数N"))
ReDim x(n)
Randomize
For i = 1 To n
x(i) = Int(Rnd * 50)
Next i
End Sub
Private Sub Command2_Click()
Call sum
Print "奇数之和=" & a
Print "偶数之和=" & b
End Sub