textbox 数组
利用随机函数产生100个10~99之间的随机整数,找出其中的素数,并将这些素数求和。1.将随机产生的100个整数存入数组,按每行10个数输出到文本框中。2.用Sub子过程...
利用随机函数产生100个10~99之间的随机整数,找出其中的素数,并将这些素数求和。
1. 将随机产生的100个整数存入数组,按每行10个数输出到文本框中。
2. 用Sub子过程或Function函数过程判断随机整数是否为素数。
3. 将素数按每行10个数在文本框中输出。
4. 将素数的个数以及求和结果出到文本框中。
5. 界面自行设计。 展开
1. 将随机产生的100个整数存入数组,按每行10个数输出到文本框中。
2. 用Sub子过程或Function函数过程判断随机整数是否为素数。
3. 将素数按每行10个数在文本框中输出。
4. 将素数的个数以及求和结果出到文本框中。
5. 界面自行设计。 展开
展开全部
'建一个Command1控件,两个Text控件,Text控件宽度和高度都拉大一点,且MultiLine属性均设为True
Private Function prime(ByVal x As Integer) As Boolean '判断是否素数
Dim i As Integer
For i = 2 To Sqr(x)
If x Mod i = 0 Then
Exit Function
End If
Next i
prime = True
If x < 2 Then prime = False
End Function
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
n = 0
psum = 0
Randomize
For i = 1 To 100
a(i) = 10 + Int(90 * Rnd)
s1 = s1 & " " & a(i)
If prime(a(i)) Then
s2 = s2 & " " & a(i)
n = n + 1
psum = psum + a(i)
If n Mod 10 = 0 Then s2 = s2 & vbCrLf
End If
If i Mod 10 = 0 Then s1 = s1 & vbCrLf
Next
Text1.Text = s1
Text2.Text = s2 & vbCrLf & "素数个数为" & n & vbCrLf & "素数之和为" & psum
End Sub
Private Function prime(ByVal x As Integer) As Boolean '判断是否素数
Dim i As Integer
For i = 2 To Sqr(x)
If x Mod i = 0 Then
Exit Function
End If
Next i
prime = True
If x < 2 Then prime = False
End Function
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
n = 0
psum = 0
Randomize
For i = 1 To 100
a(i) = 10 + Int(90 * Rnd)
s1 = s1 & " " & a(i)
If prime(a(i)) Then
s2 = s2 & " " & a(i)
n = n + 1
psum = psum + a(i)
If n Mod 10 = 0 Then s2 = s2 & vbCrLf
End If
If i Mod 10 = 0 Then s1 = s1 & vbCrLf
Next
Text1.Text = s1
Text2.Text = s2 & vbCrLf & "素数个数为" & n & vbCrLf & "素数之和为" & psum
End Sub
展开全部
'一个TextBox,设置其Multiline属性为True
'一个CommandButton
Function SU(ByVal n As Integer) As Boolean
SU = True
For i = 2 To Fix(Sqr(n))
If n Mod i = 0 Then
SU = False
Exit Function
End If
Next
End Function
Private Sub Command1_Click()
Dim a(9, 9) As Integer, iNum As Integer
Randomize
For i = 0 To 9
For j = 0 To 9
a(i, j) = Fix(Rnd * 90) + 10
Print a(i, j);
Next
Print
Next
Print
For i = 0 To 9
For j = 0 To 9
If SU(a(i, j)) Then
s = s + a(i, j)
n = n + 1
t = t + 1
If t < 10 Then
Text1.Text = Text1.Text & a(i, j) & " "
Else
Text1.Text = Text1.Text & a(i, j) & vbCrLf
t = 0
End If
End If
Next
Next
Text1.Text = Text1.Text & vbCrLf & "素数的个数:" & n & vbCrLf & "素数之和:" & s
End Sub
'一个CommandButton
Function SU(ByVal n As Integer) As Boolean
SU = True
For i = 2 To Fix(Sqr(n))
If n Mod i = 0 Then
SU = False
Exit Function
End If
Next
End Function
Private Sub Command1_Click()
Dim a(9, 9) As Integer, iNum As Integer
Randomize
For i = 0 To 9
For j = 0 To 9
a(i, j) = Fix(Rnd * 90) + 10
Print a(i, j);
Next
Next
For i = 0 To 9
For j = 0 To 9
If SU(a(i, j)) Then
s = s + a(i, j)
n = n + 1
t = t + 1
If t < 10 Then
Text1.Text = Text1.Text & a(i, j) & " "
Else
Text1.Text = Text1.Text & a(i, j) & vbCrLf
t = 0
End If
End If
Next
Next
Text1.Text = Text1.Text & vbCrLf & "素数的个数:" & n & vbCrLf & "素数之和:" & s
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询