zoukankan      html  css  js  c++  java
  • vbs习题

    练习题:

    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>=48 And b<=57 Then
        c=0
    ElseIf b>=65 And b<=90 Then
        c=1
    ElseIf b>=97 And b<=122 Then
        c=2
    Else 
        c=3
    End if
    
    Select Case c
        Case 0
        MsgBox "输入为数字:"&a
        Case 1
        MsgBox "输入为大写字母:"&a
        Case 2
        MsgBox "输入为小写字母:"&a
        Case 3
        MsgBox "输入为其它符号:"&a
    End Select

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

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

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

    Option Explicit  
    Dim i,j,usernum(9),tempnum
    For i=0 To 9 
        usernum(i)=CInt(InputBox("请输入第"&i+1&"个数"))
    Next
    For i=0 To 8 
      For j=0 To 8-i Step 1
        If usernum(j)>usernum(j+1) Then
            tempnum=usernum(j)
            usernum(j)=usernum(j+1)
            usernum(j+1)=tempnum
        End If
      Next
    Next
    For i=0 To 9 
        MsgBox(usernum(i))
    Next
    '方法二
    Dim a(10)
    For i=1 To 10 
         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=1 To 10
        For j=1 To 10-i
            change a(j),a(j+1)
        next
    Next
    MsgBox a(10)&","&a(9)&","&a(8)&","&a(7)&","&a(6)&","&a(5)&","&a(4)&","&a(3)&","&a(2)&","&a(1)

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

    Dim strUsername,strPassword
    Do 
    strUsername=CStr(InputBox("please input your UserName!","Input Name"))
        If Len(strUsername)<4 or Len(strUsername)>10 Then
            MsgBox "用户名必须是4-10位字符!请重新输入!",48,"用户名输入不正确!"
        Else
            Exit Do
        End If
    Loop
    
    x=0
    Do 
    strPassword=CStr(InputBox("please input the password!","Input password"))
    strPassword=LCase(strPassword)
        If strPassword="mercury" Then
            MsgBox "用户"&strUsername&"登陆成功!",48,"登陆成功!"
            Exit Do
          Else
              MsgBox "密码不正确!",48,"密码不正确!"
              x=x+1
              If x=3 Then
                  MsgBox "请重新登陆!",48,"请重新登陆!"
                  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        '初始化随机生成器,后面的值5也可以省略而是用系统的种子
        MsgBox arrg(Int(5*Rnd))  '随机输出数组中的元素值
    Next

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

    Function Writelog(str)
        Const ForReading=1,ForWriting=2,ForAppending=8
        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=1,ForWriting=2,ForAppending=8
    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 1,2,"yezhaohui"
  • 相关阅读:
    圆圈中最后剩下的数字 【微软面试100题 第十八题】
    第一个只出现一次的字符 【微软面试100题 第十七题】
    从上往下打印二叉树 【微软面试100题 第十六题】
    二叉树的镜像 【微软面试100题 第十五题】
    和为s的两个数字 【微软面试100题 第十四题】
    链表中倒数第k个结点 【微软面试100题 第十三题】
    求1+2+...+n 【微软面试100题 第十二题】
    求二叉树中结点的最大距离 【微软面试100题 第十一题】
    翻转句子中单词的顺序 【微软面试100题 第十题】
    二叉搜索树的后序遍历序列 【微软面试100题 第九题】
  • 原文地址:https://www.cnblogs.com/yezhaohui/p/3868169.html
Copyright © 2011-2022 走看看