求1000以内素数的vb程序设计
以下是我自己写的,可是运行时总是出现对话框“只有next没有for”PrivateSubCommand1_Click()DimiAsInteger,xAsInteger,...
以下是我自己写的,可是运行时总是出现对话框“只有next没有for” Private Sub Command1_Click() Dim i As Integer, x As Integer, b As Boolean For x = 1 To 999 b = True For i = 1 To Sqr(x) If x Mod i = 0 Then Exit For Next i If b Then List1.AddItem (x) End If Next End Sub Private Sub Command2_Click() Text1.Text = List1.ListCount End Sub Private Sub Command3_Click() End End Sub 希望能指点一下哪里错了,或者写出相关程序,不胜感激!
展开
展开全部
你的代码有几处错误,正确代码如下: Private Sub Command1_Click() Dim i As Integer, x As Integer, b As Boolean For x = 2 To 999 'x=1改为x=2,因为最小素数是2 b = True '假设x是素数 For i = 2 To Sqr(x) 'i=1应改为i=2 If x Mod i = 0 Then b = False '如果能整除则x不是素数 Exit For End If '你的代码中少了End if Next i If b Then List1.AddItem x End If Next x End Sub Private Sub Command2_Click() Text1.Text = List1.ListCount End Sub Private Sub Command3_Click() End End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询