VBS练习题

时间:2024-01-16 08:59:26

练习题:

1、输入3个数,输出其中最大的那个值。

Option Explicit
Dim intA,intB,intC
intA=CInt(InputBox("请输入a:"))
intB=CInt(InputBox("请输入b:"))
intC=CInt(InputBox("请输入c:"))
If intA>intB And intA>intC Then
MsgBox "最大值是:"&intA
ElseIf intB>intC Then
MsgBox "最大值是:"&intB
Else
MsgBox "最大值是:"&intC
End If

2、判断输入的字符类型。

Dim a,b,c
a=InputBox("请输入一个字符:")
b=Asc(a)
If b>= And b<= Then
c=
ElseIf b>= And b<= Then
c=
ElseIf b>= And b<= Then
c=
Else
c=
End if Select Case c
Case
MsgBox "输入为数字:"&a
Case
MsgBox "输入为大写字母:"&a
Case
MsgBox "输入为小写字母:"&a
Case
MsgBox "输入为其它符号:"&a
End Select

3、输入一组数字,倒序输出。

Dim a()
For i= To
a(i)=InputBox("请输入第"&i+&"数字:")
Next
MsgBox "你输入的数字倒序输出为:"&a()&","&a()&","&a()&","&a()&","&a()

4、用VBS实现冒泡排序,输入10个数,按从小到大或从大到小的顺序排列。

Option Explicit  
Dim i,j,usernum(),tempnum
For i= To
usernum(i)=CInt(InputBox("请输入第"&i+&"个数"))
Next
For i= To
For j= To -i Step
If usernum(j)>usernum(j+) Then
tempnum=usernum(j)
usernum(j)=usernum(j+)
usernum(j+)=tempnum
End If
Next
Next
For i= To
MsgBox(usernum(i))
Next
'方法二
Dim a()
For i= To
a(i)=cint(InputBox("请输入第"&i&"数字:"))
Next
Function change(x,y)
If x>y Then
tem=x
x=y
y=tem
End If
End Function
For i= To
For j= To -i
change a(j),a(j+)
next
Next
MsgBox a()&","&a()&","&a()&","&a()&","&a()&","&a()&","&a()&","&a()&","&a()&","&a()

5、用VBS实现用户名和密码的输入验证,先输入用户名再输入密码:用户名必须是4~10位的字符,否则提示用户名为空、少于4位或多于10位。密码必须是Mercury(不区分大小写),如果输入为空则提示用户输入密码,如果连续三次未输入正确密码则提示用户重新登录,然后退出。

Dim strUsername,strPassword
Do
strUsername=CStr(InputBox("please input your UserName!","Input Name"))
If Len(strUsername)< or Len(strUsername)> Then
MsgBox "用户名必须是4-10位字符!请重新输入!",,"用户名输入不正确!"
Else
Exit Do
End If
Loop x=
Do
strPassword=CStr(InputBox("please input the password!","Input password"))
strPassword=LCase(strPassword)
If strPassword="mercury" Then
MsgBox "用户"&strUsername&"登陆成功!",,"登陆成功!"
Exit Do
Else
MsgBox "密码不正确!",,"密码不正确!"
x=x+
If x= Then
MsgBox "请重新登陆!",,"请重新登陆!"
Exit do
End If
End If
Loop

6、计算两个数求余的结果,要求:计算出错时输出错误。

Option Explicit
On Error Resume Next Function getmod(a,b)
getmod=a Mod b
End Function Dim c,ua,ub
ua=CInt(InputBox("请输入一个数"))
ub=CInt(InputBox("请再输入一个数"))
c=getmod(ua,ub)
If Err Then '可以自主产生错误 Err.Raise 6
MsgBox Err.Number &"---"& Err.Description
Err.Clear
Else
MsgBox c
End If

7、随机数。

Dim strInput,arrg
strIput=InputBox("请输入5个词语,用,分开!")
arrg=Split(strIput,",")
For Each element In arrg
Randomize '初始化随机生成器,后面的值5也可以省略而是用系统的种子
MsgBox arrg(Int(*Rnd)) '随机输出数组中的元素值
Next

8、写日志到文本文件中。

Function Writelog(str)
Const ForReading=,ForWriting=,ForAppending=
Dim fso,fil,msg
' 创建一个文件系统对象(File System Object)
Set fso = CreateObject("Scripting.FileSystemObject")
' 创建一个文件对象,通过fso对象来打开指定的文件
Set fil = fso.OpenTextFile("C:\log.txt",ForAppending)
fil.WriteLine now &" "&str
' 关闭这个文件
fil.Close
' 释放这个文件对象
Set fil = Nothing
' 释放这个文件系统对象
Set fso = Nothing
End Function Writelog "hello lxl"

9、读文本文档.txt文件并显示出来。

Option Explicit
Const ForReading=,ForWriting=,ForAppending=
Dim fso,fil,msg
' 创建一个文件系统对象(File System Object)
Set fso = CreateObject("Scripting.FileSystemObject")
' 创建一个文件对象,通过fso对象来打开指定的文件
Set fil = fso.OpenTextFile("C:\f.txt",ForReading)
' 读取文件内容
' MsgBox fil.ReadAll ' 一次性全部读取
' 判断是否到了文件的最后面
Do While Not fil.AtEndOfLine '注意和AtEndOfLine的区别
' 只要没到最后,就读取一行,同时把游标向下移动一行
msg = msg & vbNewLine & fil.ReadLine
Loop
MsgBox msg
' 关闭这个文件
fil.Close
' 释放这个文件对象
Set fil = Nothing
' 释放这个文件系统对象
Set fso = Nothing

4、读取数据库查询的结果

Dim Cnn,Rst,strCnn,Msg, Sqlstr
strCnn="Provider=MSDASQL.1;Persist Security Info=False;Data Source=calc" '数据源字符串
Set Cnn=CreateObject("ADODB.Connection")
Cnn.Open strCnn
Set Rst=CreateObject("ADODB.RecordSet")
Sqlstr="select * from calc order by TestNumber1 asc"
Rst.Open Sqlstr,Cnn
Rst.MoveFirst
Do While Not Rst.EOF
Msg=Msg&vbTab&Rst.Fields("TestNumber1")&vbTab&Rst.Fields("TestNumber2")&vbTab&Rst.Fields("TestResult")&vbNewLine
Rst.MoveNext
Loop
Rst.Close
Cnn.Close
Set Rst=Nothing
Set Cnn=Nothing
MsgBox Msg

5、更新数据库中的数据

Dim Cnn,strCnn,Cmd,Sqlstr
strCnn=" Provider=MSDASQL.1;Persist Security Info=False;Data Source=calc "
Set Cnn=CreateObject("ADODB.Connection")
Cnn.Open strCnn
'Set Rst=CreateObject("ADODB.RecordSet")
Set Cmd=CreateObject("ADODB.Command")
Cmd.ActiveConnection = Cnn
Sqlstr="update calc set TestNumber1= 78901 where TestNumber1= 78900" Cmd.CommandText = Sqlstr
Cmd.Execute
Cnn.Close
Set Rst=Nothing
Set Cnn=Nothing

6、插入数据到数据库中

Sub inputaccess(tn1,tn2,tr)
Dim Cnn,strCnn,Cmd,Sqlstr
strCnn="Provider=MSDASQL.1;Persist Security Info=False;Data Source=lxl"
Set Cnn=CreateObject("ADODB.Connection")
Cnn.Open strCnn
Set Cmd=CreateObject("ADODB.Command")
Cmd.ActiveConnection = Cnn
Sqlstr="insert into calc(TestNumber1,TestNumber2,TestResult) values("&tn1&","&tn2&",'"&tr&"')"
Cmd.CommandText = Sqlstr
Cmd.Execute
Cnn.Close
Set Rst=Nothing
Set Cnn=Nothing
End Sub
inputaccess ,,"yezhaohui"