vb 哪里有问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 01:10:03
求最大公约数的代码

Dim max As Integer
Dim min As Integer

Sub GetMaxMin(ByVal m As Integer, ByVal n As Integer)
Dim i As Integer

If m > n Then
For i = n To 1 Step -1
If m Mod i = 0 And n Mod i = 0 Then
max = i
End If
Exit For
Next i
ElseIf n > m Then
For i = m To 1 Step -1
If m Mod i = 0 And n Mod i = 0 Then
max = i
End If
Exit For
Next i
Else
max = m
End If

End Sub

Private Sub Command1_Click()
Call GetMaxMin(75, 65)
Print max
End Sub

帮你改好了

Dim max As Integer
Dim min As Integer

Private Sub Form_Load()
End Sub
Sub GetMaxMin(ByVal m As Integer, ByVal n As Integer)
Dim i As Integer
If m > n Then
For i = n To 1 Step -1
If (m Mod i = 0) And (n Mod i = 0) Then
max = i
Exit For
End If
Next i

ElseIf n > m Then
For i = m To 1 Step -1
If m Mod i = 0 And n Mod i = 0 Then
max = i
Exit For
End If
Next i
Else
max = m
End If

End Sub

Private Sub Command1_Click()
Call GetMaxMin(75, 65)
Print max
End Sub

Exit For的位置不对
Exit For应该是找到了最大公约数就跳出循环不再找了,所以Exit For应该放在max=i之后endif之前。
现在Exit For是被放在了for循环中了,所以for循环只运行了一次就跳出for了。