vb改错大题
图1
含有错误的源程序如下:
Option Explicit
Private Sub Command1_Click()
Dim i As Integer, k As Integer, st As String
For i = 10 To 500
If wanshu(i) Then
st = CStr(i)
Text1.Text = Text1.Text & st
Call test(st)
Text1.Text = Text1.Text & Chr(13) & Chr(10)
End If
Next i
End Sub
Private Function wanshu(n As Integer) As Boolean
Dim i As Integer, sum As Integer
For i = 2 To n - 1
If n Mod i = 0 Then
sum = sum + i
End If
Next i
If sum = n Then wanshu = True
End Function
Private Sub test(s As String)
Dim i As Integer, sum As Integer
sum = 0
Do While Len(s) > 1
For i = 1 To Len(s)
sum = sum + Val(mid(s, i, 1))
Next i
Text1.Text = Text1.Text & "->" & Str(sum)
s = Str(sum)
Loop
End Sub 展开
Option Explicit
Private Sub Command1_Click()
Dim i As Integer, k As Integer, st As String
For i = 10 To 500
If wanshu(i) Then
st = CStr(i)
Text1.Text = Text1.Text & st
Call test(st)
Text1.Text = Text1.Text & Chr(13) & Chr(10)
End If
Next i
End Sub
Private Function wanshu(n As Integer) As Boolean
Dim i As Integer, sum As Integer
sum = 1
wanshu = False
For i = 2 To n - 1
If n Mod i = 0 Then
sum = sum + i
End If
Next i
If sum = n Then wanshu = True
End Function
Private Sub test(s As String)
Dim i As Integer, sum As Integer
Do While Len(s) > 1
sum = 0
For i = 1 To Len(s)
sum = sum + Val(Mid(s, i, 1))
Next i
Text1.Text = Text1.Text & "->" & Str(sum)
s = CStr(sum)
Loop
End Sub
Option Explicit
Private Sub Command1_Click()
Dim i As Integer, k As Integer, st As String
For i = 10 To 500
If wanshu(i) Then
st = CStr(i)
Text1.Text = Text1.Text & st
Call test(st)
Text1.Text = Text1.Text & Chr(13) & Chr(10)
End If
Next i
End Sub
Private Function wanshu(n As Integer) As Boolean
Dim i As Integer, sum As Integer
For i = 1 To n - 1 '检验完数应从1开始
If n Mod i = 0 Then
sum = sum + i
End If
Next i
If sum = n Then wanshu = True
End Function
Private Sub test(s As String)
Dim i As Integer, sum As Integer
Do While Len(s) > 1
sum = 0 '每次计算各位之和,应把累计数清零
For i = 1 To Len(s)
sum = sum + Val(Mid(s, i, 1))
Next i
Text1.Text = Text1.Text & "->" & Str(sum)
s = Trim(Str(sum)) ' Str函数在讲sum转换为字符串的时候,字符多加了一个" "导致Len(s)始终大于1,使用trim函数去掉多余的空格
Loop
End Sub