中国好声音第四季单良:为什么这两段ASP代码的运行结果会不同?

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 05:31:29
<%dim a(6)
for i=0 to 6
a(i)=i+1
next
for i=0 to 6
count=count+a(i)+1
next
response.write (count)
%>

<%
dim a
for i=0 to 6
a=i+1
next
for i=0 to 6
count=count+a+1
next
response.write (count)%>
为什么这两段ASP代码的运行结果会不同?


a(0)=1
a(1)=2
a(2)=3
a(3)=4
a(4)=5
a(5)=6
a(6)=7

count=1+1=2
count=2+2+1=5
count=5+3+1=9
count=9+4+1=14
count=14+5+1=20
count=20+6+1=27
count=27+7+1=35

count=35


a=0+1=1
a=1+1=2
a=2+1=3
a=3+1=4
a=4+1=5
a=5+1=6
a=6+1=7

最后a=7 没有累计加

count=7+1=8 循环7次 累加
count=8+7+1=16
count=16+7+1=24
count=24+7+1=32
count=32+7+1=40
count=40+7+1=48
count=48+7+1=56

最后count=56

当然不同..

第一段是数组,A(0)~A(6)是渐渐增大的,可以说结果是A(0)+..+A(6)+1

第二段看起来你的A是不断在变化的.事实上你最后算的时候,只是算最后的A值.
也就是A+...+A+1
结果当然不同.全部都是加最后一个A

@@,还有一个问题,你的count没有初始化,在运行中取值也是有可能不同的!

第一个程序:
a(0)=1;a(1)=2;a(2)=3;a(3)=4;a(4)=5;a(5)=6;a(6)=7.
第二个程序:
a=7
垒加的数不一样当然结果会不同。