联想g580拆机换屏教程:懂VB语言的高手快来

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 14:58:44
sub towstotal()
dim a ,total as integer
for a=2 to 10 step 2
total=total+a
next a
end sub
结果是多少?是2,4,6,8,10的总和吗?
我认为不是,帮忙看看下我的分析对吗?
FOR句起吧:A=2 TO 10然后每次加上2
a= 2 4 6 8 10(total+a的total为total1吧)
total1= 0 2 6 12 20
total = 2 6 12 20 30
所以最后结果为2 6 12 20 30的总和,请问这样对吗?为什么?
谢谢呀

total像一个布袋,a就像豆子。
第一次拿2粒豆子放进布袋,
第二次拿4粒
....
第五次拿10粒。
这样,布袋中就是30粒了。

要是像问题补充里一样说法,就是将布袋加上布袋了。
而这里只有一个布袋。

程序不太严谨,照理应先赋值于total=0,虽然也能得到结果
sub towstotal()
dim a ,total as integer
total=0
for a=2 to 10 step 2
total=total+a
next a
end sub

结果确实是2、4、6、8、10之和

-----------------------------------------
实际上这段可代码是这样理解
sub towstotal()
dim a ,total, total1 as integer
total=0
for a=2 to 10 step 2
total1=total+a
total=total1
next a
end sub
即每经过一次循环,total的值都被改写了。

是的,STEP 是步长, 循环的终值是10 ,每次-2 一直减到2 TO 2循环终止

应该这么写。

Function towstotal() As Integer
Dim a As Integer, total As Integer

total = 0
For a = 2 To 10 Step 2
total = total + a
Next

towstotal = total
End Function

是个子函数,返回值不确定,如果RETURN(TOTAL),那返回2 4 6 8 10之和.

在函数运行过程中,TOTAL会得到2 4 6 8 10之和.但函数运行结束后,变量就消失了.

结果是30
step 2是每次步长为2