VB改错题,高手快来!!!谢谢!!!!!
1.PublicSubSort1(DAsVariant,NAsLong)'从小到大对D数组内容排序,N=元素个数DimTAsVariantDimI,JAsLongForI...
1.
Public Sub Sort1(D As Variant, N As Long) '从小到大对D数组内容排序,N=元素个数
Dim T As Variant
Dim I, J As Long
For I = 1 To N
For J = 1 To N
If D(I) < D(J) Then T = D(J): D(I) = D(J): D(J) = D(I)
Next J
Loop
End Sub
2. 求1~100之间能被7整除或个位是7的整数之和。
Dim S, J As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
End If
If I Mod 10 = 7 Then
S = S + I
End If
Next S
Print S
煌_璇 - 见习魔法师 三级: 你修改的第二题的最后的next怎么没有for加上for又说没有变量.. 展开
Public Sub Sort1(D As Variant, N As Long) '从小到大对D数组内容排序,N=元素个数
Dim T As Variant
Dim I, J As Long
For I = 1 To N
For J = 1 To N
If D(I) < D(J) Then T = D(J): D(I) = D(J): D(J) = D(I)
Next J
Loop
End Sub
2. 求1~100之间能被7整除或个位是7的整数之和。
Dim S, J As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
End If
If I Mod 10 = 7 Then
S = S + I
End If
Next S
Print S
煌_璇 - 见习魔法师 三级: 你修改的第二题的最后的next怎么没有for加上for又说没有变量.. 展开
3个回答
展开全部
Dim T As Variant
Dim I, J As Long
For I = 1 To N
For J = 1 To N
If D(I) < D(J) Then
T = D(J)
D(J) = D(I)
D(I) = T
End If
Next J
Next I
--------------
Dim S, J As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
ElseIf I Mod 10 = 7 Then
S = S + I
End If
Next I
Print S
Dim I, J As Long
For I = 1 To N
For J = 1 To N
If D(I) < D(J) Then
T = D(J)
D(J) = D(I)
D(I) = T
End If
Next J
Next I
--------------
Dim S, J As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
ElseIf I Mod 10 = 7 Then
S = S + I
End If
Next I
Print S
展开全部
先说说第二题:
题干要求是求1~100之间能被7整除“或”个位是7的整数之和。
而lz给的源代码上的程序显然犯了逻辑错误,lz的脚本的对应问题应该是:
求1~100之间能被7整除的整数的和 + 1~100之间个位是7的整数之和。
next后不应为s;
根据题意,应改为:
Dim S As Long, I As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
Else '此处用else是防止有一个数同时符合2个条件而加两次的行为
If I Mod 10 = 7 Then
S = S + I
End if
Next I
Print S
第一题的错误更明显,看这句:
If D(I) < D(J) Then T = D(J): D(I) = D(J): D(J) = D(I)
这句本来应该起到D(I)和D(J)互换的作用,可是这里没有起到应该起到的作用,只是把D(J)的值同时给D(I)和D(J)而已。
And程序最后少了1个next,多了个loop
并且,两个For的嵌套虽然没有错,但是就显得做了很多冗余的比较,应改为:
Public Sub Sort1(D As Variant, N As Long) '从小到大对D数组内容排序,N=元素个数
Dim T As Variant
Dim I, J As Long
For I = 1 To N-1
For J = I To N '这两个for更为精简
If D(I) > D(J) Then T = D(J): D(I) = D(J): D(J) = T
Next J
Next I
End Sub
抱歉,打字太快了……Next后面要+个I,不过不加也可以啊。
题干要求是求1~100之间能被7整除“或”个位是7的整数之和。
而lz给的源代码上的程序显然犯了逻辑错误,lz的脚本的对应问题应该是:
求1~100之间能被7整除的整数的和 + 1~100之间个位是7的整数之和。
next后不应为s;
根据题意,应改为:
Dim S As Long, I As Integer
S = 0
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
Else '此处用else是防止有一个数同时符合2个条件而加两次的行为
If I Mod 10 = 7 Then
S = S + I
End if
Next I
Print S
第一题的错误更明显,看这句:
If D(I) < D(J) Then T = D(J): D(I) = D(J): D(J) = D(I)
这句本来应该起到D(I)和D(J)互换的作用,可是这里没有起到应该起到的作用,只是把D(J)的值同时给D(I)和D(J)而已。
And程序最后少了1个next,多了个loop
并且,两个For的嵌套虽然没有错,但是就显得做了很多冗余的比较,应改为:
Public Sub Sort1(D As Variant, N As Long) '从小到大对D数组内容排序,N=元素个数
Dim T As Variant
Dim I, J As Long
For I = 1 To N-1
For J = I To N '这两个for更为精简
If D(I) > D(J) Then T = D(J): D(I) = D(J): D(J) = T
Next J
Next I
End Sub
抱歉,打字太快了……Next后面要+个I,不过不加也可以啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误多多啊
1.Dim I, J As Long ------>Dim I As Long
Dim J as long
2.For I = 1 To N ---------->For I = 1 To N-1
3. For J = 1 To N---------->For J = i+1 To N
4.If D(I) < D(J)----------->If D(I) > D(J)
4.T = D(J): D(I) = D(J): D(J) = D(I) ------>T = D(J): D(J) = D(I): D(I) = T
5.LOOP -------------->next I
==================================
1.Dim S, J As Integer---------------->Dim S As Integer
Dim I As Integer
2.
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
End If
If I Mod 10 = 7 Then
S = S + I
End If
Next S
---------------------------------->
For I = 1 To 100
If I Mod 7 = 0 or I mod 10 =7 Then
S = S + I
End If
Next I
1.Dim I, J As Long ------>Dim I As Long
Dim J as long
2.For I = 1 To N ---------->For I = 1 To N-1
3. For J = 1 To N---------->For J = i+1 To N
4.If D(I) < D(J)----------->If D(I) > D(J)
4.T = D(J): D(I) = D(J): D(J) = D(I) ------>T = D(J): D(J) = D(I): D(I) = T
5.LOOP -------------->next I
==================================
1.Dim S, J As Integer---------------->Dim S As Integer
Dim I As Integer
2.
For I = 1 To 100
If I Mod 7 = 0 Then
S = S + I
End If
If I Mod 10 = 7 Then
S = S + I
End If
Next S
---------------------------------->
For I = 1 To 100
If I Mod 7 = 0 or I mod 10 =7 Then
S = S + I
End If
Next I
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询