VB高手快进来吧,看看这段代码哪儿出错了啊!!!!!!!!!!!

2024-12-28 07:36:57
推荐回答(5个)
回答1:

1、你在前面调用Call Arms(i, f, a, n),但在定义过程“Arms”时,“k As Integer”前面未添加“ByVal”(按值传递参数),因此是按地址传递参数,而在过程“Arms”中的“k = k \ 10”处更改了k的值,导致的结果就是改变了Command1_Click过程中调用处的“i”的值,导致无限循环。
2、过程“Arms”中“sum As Integer”是否应改为“sum As Long”,否则很容易溢出错误。

回答2:

Option Explicit
Option Base 1
Private Sub Command1_Click()
Dim i As Integer, a() As Integer, f As Boolean, n As Integer
Dim j As Integer, st As String

For i = 153 To 9999
f = False
Call Arms(i, f, a, n)
st = "" '********************************位置改
If f Then
st = st & i & "="
For j = 1 To UBound(a) - 1
st = st & a(j) & "^" & n & "+"
Next j
st = st & a(j) & "^" & n
List1.AddItem st
End If
Next i
End Sub

Private Sub Arms(ByVal k As Integer, f As Boolean, b() As Integer, n As Integer)‘******加ByVal
Dim i As Integer, sum As Integer, m As Integer
n = Len(CStr(k)) ’**************************str---->cstr
m = k
ReDim b(n)
For i = n To 1 Step -1
b(i) = k Mod 10
k = k \ 10
Next i
For i = 1 To n
sum = sum + b(i) ^ n
Next i
If sum = m Then f = True

End Sub

回答3:

1.
Call Arms(i, f, a, n)
改称
f = Arms(i, f, a, n)

2。
Private Sub Arms(k As Integer, f As Boolean, b() As Integer, n As Integer) As Boolean
.............................
.......................

'最后加这一句
reutrn f
End Sub

回答4:

Option Explicit
Option Base 1
Private Sub Command1_Click()
Dim i As Integer, a() As Integer, f As Boolean, n As Integer
Dim j As Integer, st As String
st = ""
For i = 153 To 9999
f = False
Call Arms((i), f, a, n)
If f Then
st = st & i & "="
For j = 1 To UBound(a) - 1
st = st & a(j) & "^" & n & "+"
Next j
st = st & a(j) & "^" & n
List1.AddItem st
End If
Next i
End Sub

Private Sub Arms(k As Integer, f As Boolean, b() As Integer, n As Integer)
Dim i As Integer, sum As Long, m As Integer
n = Len(Str(k))
m = k
ReDim b(n)
For i = n To 1 Step -1
b(i) = k Mod 10
k = k \ 10
Next i
For i = 1 To n
sum = sum + b(i) ^ n
Next i
If sum = m Then f = True

End Sub

可以百度HI找我
加入百度HI vb6专家群1358368
qq vb6交流群 184734495

回答5:

这有答案:http://wenku.baidu.com/view/e387b580ec3a87c24028c495.html