China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-04 06:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please have a VBS expert analyze why this piece of VBS code can't calculate the sum View 793 Replies 7
Original Poster Posted 2007-04-22 09:54 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
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 ]
Floor 2 Posted 2007-04-22 10:03 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
I posted a thread with no technical content...........

[ Last edited by stornager on 2007-5-24 at 11:53 PM ]
Floor 3 Posted 2007-04-22 10:47 ·  中国 福建 福州 连江县 电信
中级用户
★★
Credits 356
Posts 115
Joined 2004-07-27 00:00
22-year member
UID 29114
Gender Male
Status Offline
Floor 4 Posted 2007-04-22 10:59 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
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
Floor 5 Posted 2007-04-22 11:09 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The loop counter in For can't participate in the calculation
Dim sum,n
sum=0
n=Inputbox("Please enter a number. ")
For i = 1 to n
sum=sum+1
Next
Msgbox sum


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
Floor 6 Posted 2007-04-22 11:37 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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.
Floor 7 Posted 2007-04-22 13:08 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
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
Floor 8 Posted 2007-04-23 05:08 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
Thanks to all you experts for your guidance, I finally figured it out.
Forum Jump: