为什么会“益出”
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim s As Integer
For i = 2 To 1000
For j = 1 To i
s = s + j →为什么这里会益出,那里出错了?
If i = s Then
Print i
Exit For
End If
Next
Next
End Sub
[解决办法]
你EXIT FOR 循环的条件判断式在双循环结束前不会满足,建议代码修改如下:
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim s As Integer
For i = 2 To 1000
For j = 1 To i
s = s + j '→为什么这里会益出,那里出错了?
Debug.Print , s
If i < s Then
Print i
Exit For
End If
Next
Next
End Sub
[解决办法]
Dim i As Long
Dim j As Long
Dim s As Long
For i = 2 To 1000
List1.Clear
For j = 1 To i
List1.AddItem CStr(j)
List1.AddItem CStr(i)
s = s + j
If i = s Then
Print i
Exit For
End If
Next
Next
[解决办法]
不明白程序的意图。按照原先的程序,就算不溢出,也没有结果。
Option Explicit
Sub main()
Dim i As Integer
Dim j As Integer
Dim s As Long
For i = 2 To 1000
For j = 1 To i
s = s + j ' →为什么这里会益出,那里出错了?
If i = s Then
Debug.Print i
Exit For
End If
Next
Next
End Sub
结果是空白