用VBS脚本 写出任意输入两个整数,求这两个整数之间所有整数的和
没事自学写着玩的,折腾了一晚上,写不出来,大神帮帮忙吧
VBS有点过时了,建议学习C#或者Java什么的。
假如要学习VBS,可以学习VB(因为VBS的语法就是VB的语法),使用开发工具VB6.0,方便调试。
回答问题:输入两个整数,求这两个整数之间所有整数的和。
1首先判断两个整数谁大谁小
2写一个遍历,从小的数开始,到大的数结束,每次遍历临时变量加1
3用一个新的变量保存结果,把遍历过程的数加起来。
这个程序在windows10 下测过:
stdout_in.vbs:
Wscript.StdOut.Write "Enter the first number: "
strNumber1 = Wscript.StdIn.ReadLine
Wscript.StdOut.Write "The first number entered is: " & strNumber1&chr(13)&chr(10)
Wscript.StdOut.Write "Enter the second number: "
strNumber2 = Wscript.StdIn.ReadLine
Wscript.StdOut.Write "The second number entered is: " & strNumber2&chr(13)&chr(10)
Dim counter1,counter2,sum
'store the smaller number in counter1
if strNumber1>strNumber2 Then
counter1=strNumber2
counter2=strNumber1
else
counter1=strNumber1
counter2=strNumber2
end if
dim x
For x = counter1 to counter2 step 1
sum=sum+x
Wscript.StdOut.Write x&chr(13)&chr(10)
Next
Wscript.StdOut.Write "The sum of all integer between:" & counter1&" and "&counter2&" is:"&sum&chr(13)&chr(10)
使用方法:在windows下命令行格式下,输入:
C:\Users\tom\Documents\reference\VBscript\examples>cscript stdout_in.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
Enter the first number: 10
The first number entered is: 10
Enter the second number: 20
The second number entered is: 20
10
11
12
13
14
15
16
17
18
19
20
The sum of all integer between:10 and 20 is:165
C:\Users\tom\Documents\reference\VBscript\examples>
这个例子中输入10,20, 求得中间整数和。