中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-04 07:47
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » Please have a VBS expert analyze why this piece of VBS code can't calculate the sum
Printable Version  795 / 7
Floor1 stornager Posted 2007-04-22 09:54
中级用户 Posts 131 Credits 328
Option Explicit
Dim sum,n
sum=0
n=Inputbox("Please enter a number: ")
For num=1 to n
sum=sum+num
Next
Msgbox sum

The code above can now calculate the sum!!!!, haha!!

[ Last edited by stornager on 2007-5-24 at 11:51 PM ]
Floor2 stornager Posted 2007-04-22 10:03
中级用户 Posts 131 Credits 328
I posted a thread with no technical content...........

[ Last edited by stornager on 2007-5-24 at 11:53 PM ]
Floor3 balinger Posted 2007-04-22 10:47
中级用户 Posts 115 Credits 356
Floor4 baomaboy Posted 2007-04-22 10:59
银牌会员 Posts 554 Credits 1,513
Originally posted by stornager at 2007-4-22 09:54:
Option Explicit
Dim sum,n
sum=0
n=Inputbox("Please enter a number. ")
For sum=1 to n
sum=sum+1
Next
Msgbox sum
:o:o:o


Your problems are basically all algorithm-related, but actually they're all syntax problems. I suggest you first take a look at "VBScript Language Reference.chm"; maybe this kind of problem... sigh
Floor5 zh159 Posted 2007-04-22 11:09
金牌会员 Posts 1,467 Credits 3,687
The loop counter in For can't participate in the calculation


For sum = 1 to n
sum=sum+1
Here, each time sum is calculated it gets assigned 1 to n by For sum = 1 to n, and then sum+1, so the calculation is incorrect
Floor6 slore Posted 2007-04-22 11:37
铂金会员 Posts 2,478 Credits 5,212
Originally posted by baomaboy at 2007-4-21 21:59:

Your problems are basically all algorithm-related, but actually they're all syntax problems. I suggest you first take a look at "VBScript Language Reference.chm"; maybe this kind of problem... sigh



It doesn't seem to be a syntax problem...



If it's just for calculating that, then I suggest:
Dim sum,n
n=Inputbox("Please enter a Number. ")
sum=(1+n)*n/2
Msgbox sum


If this is about applying a FOR loop, then pretend I didn't say anything.
Floor7 baomaboy Posted 2007-04-22 13:08
银牌会员 Posts 554 Credits 1,513
Originally posted by slore at 2007-4-22 11:37:



It doesn't seem to be a syntax problem...




It can only be said that the statements themselves aren't wrong, because they were copied mechanically. So he still doesn't understand how to apply the syntax
Floor8 stornager Posted 2007-04-23 05:08
中级用户 Posts 131 Credits 328
Thanks to all you experts for your guidance, I finally figured it out.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023