在窗体Form1上绘制两个标签,标签的标题分别是“输入该数:”,“获得结果:”。绘制一个文本框,文本框中内容为空。绘制一个命令按钮,命令按钮的标题是“确定”。以及一个图片框(PictureBox,在工具箱中文本框控件的上面)。程序运行后,在文本框中输入一个正整数,单击“确定”按钮,计算出大于该数的15个素(质)数。将这15个素(质)数输出在图片框中(要求每行显示5个)

解决方案 »

  1.   

    大家帮下忙嘛 要用DO LOOP 循环 我不会啊
      

  2.   

    Sub getprime(ByVal num As Long)
    num = num + num Mod 2 + 1
    Dim p, i As Integer, prime As Boolean, n As Long, k As Long
    p = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)
    n = UBound(p)
    For i = 101 To Int(Sqr(num)) + 100 Step 2
    prime = True
    For j = 1 To n
    If i Mod p(j) = 0 Then
    prime = False
    Exit For
    End If
    Next
    If prime = True Then
    n = n + 1
    ReDim Preserve p(n)
    p(n) = i
    End If
    NextDo While k < 15prime = True
    For i = 1 To n
    If num Mod p(i) = 0 Then
    prime = False
    Exit For
    End If
    NextIf prime = True Then
    If k Mod 5 = 0 Then Picture1.Print
    Picture1.Print num;
    k = k + 1
    End If
    num = num + 2
    Loop
    End SubPrivate Sub Command1_Click()
    getprime Val(Text1.Text)
    End Sub
      

  3.   

    完整代码(新建工程,向FORM1拷贝下面代码就可以了)Private WithEvents label1 As Label
    Private WithEvents label21 As Label
    Private WithEvents text1 As TextBox
    Private WithEvents picture1 As PictureBox
    Private WithEvents command1 As CommandButton
    Private Sub Form_Load()
    Me.Move 0, 0, 6500, 4500
    Set label1 = Controls.Add("vb.label", "label1", Me)
    Set Label2 = Controls.Add("vb.label", "label2", Me)
    Set text1 = Controls.Add("vb.Textbox", "text1", Me)
    Set picture1 = Controls.Add("vb.picturebox", "picture1", Me)
    Set command1 = Controls.Add("vb.commandbutton", "command1", Me)
    label1.Move 0, 0, 1000, 500
    label1.Visible = True
    label1.Caption = "输入该数:"
    text1.Move 1000, 0, 5000, 500
    text1.Visible = True
    text1.Text = "987654321"
    Label2.Move 0, 1000, 1000, 500
    Label2.Visible = True
    Label2.Caption = "获得结果:"
    picture1.Move 1000, 1000, 5000, 1500
    picture1.Visible = True
    command1.Move 0, 3000, 6000, 500
    command1.Visible = True
    command1.Caption = "确定"
    End Sub
    Private Sub Command1_Click()
    picture1.Cls
    Dim num As Long
    num = Val(text1.Text)
    num = num + num Mod 2 + 1
    Dim p, i As Integer, prime As Boolean, n As Long, k As Long
    p = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)
    n = UBound(p)
    For i = 101 To Int(Sqr(num)) + 100 Step 2
    prime = True
    For j = 1 To n
    If i Mod p(j) = 0 Then
    prime = False
    Exit For
    End If
    Next
    If prime = True Then
    n = n + 1
    ReDim Preserve p(n)
    p(n) = i
    End If
    NextDo While k < 15prime = True
    For i = 1 To n
    If num Mod p(i) = 0 Then
    prime = False
    Exit For
    End If
    Next
    If prime = True Then
    If k = 5 Or k = 10 Then picture1.Print
    picture1.Print num;
    k = k + 1
    End If
    num = num + 2
    Loop
    End Sub
      

  4.   

    只要验证 2 to sprt(n) 不会被n整除,那n就是素数了。
    现在的cpu都这么nb,不用太关注运行速度拉
      

  5.   

    Private Sub Command1_Click()
    a = Val(Text1.Text)
    n = 0
    Do
    a = a + 1
      For i = 2 To a - 1
    If a Mod i = 0 Then Exit For
      Next i
      If i > a - 1 Then
    Picture1.Print a;
    n = n + 1
        If n Mod 5 = 0 Then
    Picture1.Print
        End If
      End If
    If n = 15 Then Exit Do
    Loop
    End Sub
      

  6.   

    我以前代过计算机二级,
    那些文科生学了VB二级都会写
    建议LZ好好学些基本功