VBScript学习笔记
本文参考:VBS基础教程。学习了vbs的基础语法,进行了一些练习,下面是我编写的demo。
1.输入输出
vbs01.vbs
REM
和‘
单引号作用都是用于注释,相当于c语言中"//"。Dim
用来声明变量,vbs支持动态数据类型,即不用事先声明变量的类型。Inputbox
作用是打开对话框,获取用户输入,它返回的数据类型是字符串,Msgbox
则用于显示。
REM 输入姓名并回显
Dim name
Dim msg
msg = "please input your name:"
name = Inputbox(msg, "Inputbox", "zhangsan") ‘param1是提示用户的语句,param2是对话框标题,param3是输入栏默认值。
Msgbox name
vbs02.vbs
REM 输入长方形的长和宽,计算其面积并输出
Dim width, length, area
Dim prompt
prompt = "please input width:"
width = Inputbox(prompt)
prompt = "please input length:"
length = Inputbox(prompt)
area = width * length
Msgbox area
vbs03.vbs
REM 输入圆的半径,计算圆面积并输出
const PI = 3.14 ‘const关键字限制PI在之后的程序中无法被修改
Dim r, area
Dim prompt
prompt = "please input r:"
r = Inputbox(prompt)
area = PI * r^2 ‘ “^”代表n的几次方,c语言中代表xor(异或)。
Msgbox area
2.条件控制语句
vbs04.vbs
REM 学习条件控制语句
Dim num1, num2
Dim prompt
prompt = "please input num1:"
num1 = CDbl(Inputbox(prompt)) ‘CDbl用于将返回值数据类型转换为数字(整形或浮点型)
prompt = "please input num2:"
num2 = CDbl(Inputbox(prompt))
if num1 > num2 then
Msgbox "num1 > num2"
else if num1 = num2 then
Msgbox "num1 = num2"
else
Msgbox "num1 < num2"
end if
end if
注:if
和end if
数量要一致,否则会造成语法错误。
vbs05.vbs
REM 学习switch和case结构
Dim n
Dim prompt
prompt = "please input a num between 1 and 3"
n = int(Inputbox(prompt))
select case n
case 1
Msgbox "happy"
case 2
Msgbox "lucky"
case 3
Msgbox "nervous"
case else
Msgbox "input error"
end select
3.循环语句(do...loop,do while...loop,do...loop while,for...next)
vbs06.vbs
REM 学习循环语句(do...loop),由用户输入密码,如果认证失败则让用户继续输入
const PWD = "123456"
dim passwd
do
passwd = Inputbox("please input your password:")
if PWD = passwd then
Msgbox "auth success!"
exit do
end if
loop
vbs07.vbs
REM 学习循环语句(do...loop),由用户输入密码,如果认证失败则让用户继续输入,认证次数上限为3次
const LIMIT = 3
const PWD = "admin_123"
Dim count, passwd
count = 0
do
passwd = Inputbox("please input your password")
if PWD = passwd then
Msgbox PWD
exit do
end if
count = count + 1
if count >= LIMIT then
Msgbox "wrong times too many!"
exit do
end if
loop
vbs08.vbs
REM 学习循环语句(do...while),由用户输入密码,如果认证失败则让用户继续输入,认证次数上限为3次
const LIMIT = 3
const PWD = "admin_123"
Dim count, passwd
count = 0
do while count < LIMIT
passwd = Inputbox("please input your password")
if PWD = passwd then
Msgbox PWD
exit do
end if
count = count + 1
‘Msgbox "wrong times " + cstr(count) ‘cstr函数将数字转换为字符串
MsgBox "wrong times " & count ‘"&"用于连接字符串
loop
vbs09.vbs
REM 学习循环语句(for...next)
Dim i, j
i = 0
j = 0
for i = 1 to 9
for j = 1 to 9
str = str & i * j & " "
next
str = str & vbCrlf ‘vbCrlf相当于回车键
next
Msgbox str
4.一维数组
vbs10.vbs
REM 学习数组使用
Dim str(2), i ‘定义数组str,括号内数字为2,但实际数组大小为3,因为下标是从0开始计算
Dim ret
for i = 0 to 2
str(i) = InputBox("please input any string")
ret = ret & str(i) & " "
next
MsgBox ret
5.函数
vbs11.vbs
REM 函数学习(function)
function Max(a, b)
if a > b then
Max = a ‘通过将变量值赋值给函数名传递函数返回值
else
Max = b
end if
end function
Dim a, b
a = int(InputBox("please input a number:"))
b = int(InputBox("please input a number:"))
MsgBox "The bigger number is " & Max(a, b)
原文地址:http://blog.51cto.com/bocaihaoci/2063415
时间: 2024-10-12 22:39:18