标题:VB代码的书写
只看楼主
泥码
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-4-18
结帖率:0
已结贴  问题点数:20 回复次数:2 
VB代码的书写
问下大神:点击按钮后创建一个长度为10的数组,随机生成10个3~100的随机整数保存在数组中。然后找出数组中的素数并打印出来,最后统计素数的个数并打印出来。这段vb代码怎写
谢谢
搜索更多相关主题的帖子: VB 数组 代码 打印 素数 
2020-04-18 19:29
时光流逝
Rank: 4
来 自:北京
等 级:业余侠客
威 望:8
帖 子:92
专家分:297
注 册:2019-11-16
得分:10 
程序代码:
Dim Number(9) As Integer, i As Integer, PrimeNum As Integer

Private Function IsPrime(Num As Integer) As Boolean
IsPrime = True
For i = 2 To Int(Sqr(Num)) + 1
    If Num Mod i = 0 Then IsPrime = False : Exit Function
Next
End Function

Private Sub CmdCreate_Click()
For i = 0 To 9
    Number(i)=Int(Rnd() * (100 - 3)) + 3
    If IsPrime(Number(i)) Then Print Number(i) : PrimeNum = PrimeNum + 1
Next
Print "素数个数:" & PrimeNum
End Sub
2020-04-19 10:39
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
得分:10 
程序代码:
Dim A(1 To 10) As Integer, B(1 To 10) As Integer, I As Integer, J As Integer, T As Integer

Private Sub Command1_Click()
Text1.Text = Text1.Text & vbCrLf & "-------------" & vbCrLf
Text1.Text = Text1.Text & "以下是素数:" & vbCrLf
For I = 1 To 10
    For J = 2 To A(I) - 1
    If A(I) Mod J = 0 Then B(I) = B(I) + 1    '检测因子的个数
    Next J
Next I
For I = 1 To 10
    If B(I) = 0 Then    '如果因子个数为0,说明是素数
    T = T + 1
    Text1.Text = Text1.Text & A(I) & Space(4)    '在文本框显示素数
    End If
Next I
Text1.Text = Text1.Text & vbCrLf & "素数有:" & T & ""
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text1.Text = "生成是10个数:" & vbCrLf
For I = 1 To 10
    Randomize
    A(I) = Int(Rnd * 97) + 3
    Text1.Text = Text1.Text & A(I) & Space(4)    '在文本框显示10个数
Next I
End Sub

以纯代码,下面是运行结果


请不要选我!!!
2020-04-19 11:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501116-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.528434 second(s), 11 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved