vb素数求和问题
题目:(事件)单击窗体。'(响应)求出100以内所有素数的和,并将结果输出'到窗体上,并存入所给变量SUM中。'-----------------------------...
题目: (事件)单击窗体。
' (响应)求出100以内所有素数的和,并将结果输出
' 到窗体上,并存入所给变量SUM中。
'-------------------------------------------------
'注意:请在指定的事件内编写代码。
'-------------------------------------------------
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
b = False
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then
b = True
End If
Next j
If b = False Then
sum = sum + i
End If
Next i
Print sum
错了!!!求解`~~` 展开
' (响应)求出100以内所有素数的和,并将结果输出
' 到窗体上,并存入所给变量SUM中。
'-------------------------------------------------
'注意:请在指定的事件内编写代码。
'-------------------------------------------------
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
b = False
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then
b = True
End If
Next j
If b = False Then
sum = sum + i
End If
Next i
Print sum
错了!!!求解`~~` 展开
1个回答
展开全部
张志晨:主要是语句的顺序不对。更正如下:
Private Sub Form_Click()
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
For i = 2 To 100
b = False
For j = 2 To i - 1
If i Mod j = 0 Then b = True
Next j
If b = False Then sum = sum + i
Next i
Print sum
End Sub
************************************************************************
Private Sub Form_Click()
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
For i = 2 To 100
b = False
For j = 2 To i - 1
If i Mod j = 0 Then
b = True
j = i '这个是我发明的迅速退出循环的绝招!
'大大加快了运行的速度!!在本例中,节省时间73.84%
End If
Next j
If b = False Then sum = sum + i
Next i
Print sum
End Sub
Private Sub Form_Click()
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
For i = 2 To 100
b = False
For j = 2 To i - 1
If i Mod j = 0 Then b = True
Next j
If b = False Then sum = sum + i
Next i
Print sum
End Sub
************************************************************************
Private Sub Form_Click()
Dim sum As Integer
'***************** Program **************
Dim i, j As Integer
Dim b As Boolean
For i = 2 To 100
b = False
For j = 2 To i - 1
If i Mod j = 0 Then
b = True
j = i '这个是我发明的迅速退出循环的绝招!
'大大加快了运行的速度!!在本例中,节省时间73.84%
End If
Next j
If b = False Then sum = sum + i
Next i
Print sum
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询