首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

为什么会“益出”解决方法

2012-01-15 
为什么会“益出”PrivateSubCommand1_Click()DimiAsIntegerDimjAsIntegerDimsAsIntegerFori2To1000Forj1To

为什么会“益出”
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


结果是空白

热点排行