2022年2022年计算机二级VB编程题目含答案 .pdf
Private Sub Command1_Click()Dim n As Integer,i As Integer,s As Double n=Val(Text1.Text)s=0 For i=1 To n s=s+i 2 Next i Text2.Text=s End Sub Private Sub Command1_Click()不得删除本行注释Dim i As Integer Open App.Path&yssj.txt For Input As#1 For i=1 To 10 Input#1,a(i)Next i Close#1 End Sub Private Sub Command2_Click()不得删除本行注释Dim i As Integer Text1.Text=For i=1 To 10 If a(i)Mod 3=2 Then Text1.Text=Text1.Text&Str(a(i)End If Next i End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 12 页 -Private Sub command1_Click()不得删除本行注释Dim a As Integer,b As Double,i As Integer,n As Integer n=Val(Text1.Text)a=0 b=0 For i=1 To n a=a+i b=b+a Next i Text2.Text=b End Sub Private Sub Command1_Click()不得删除本行注释Dim i%,xh$,xm$Open App.Path&Xhxm.txt For Output As#1 For i=1 To 3 xh=InputBox(请输入学号)xm=InputBox(请输入姓名)Write#1,xh,xm Next i Close#1 End Sub Private Sub Command2_Click()不得删除本行注释Dim s as string Open App.Path&Xhxm.txt For Input As#2 Do While Not EOF(2)Line Input#2,s Text1.Text=Text1.Text&s&vbCrLf Loop Close#2 End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 12 页 -Private Sub Command1_Click()”读取文件”按钮代码不得删除本行注释Dim st As String Text1.Text=Open App.Path&“jmwb.txt For Input As#1 While Not EOF(1)Input#1,st Text1.Text=Text1.Text+st+vbCrLf Wend Close#1 End Sub Private Sub Command2_Click()Dim s As String,co As String,c As String,Dim n As Integer,i As Integer,p As Integer s=Trim(Text1.Text)n=Len(s)co=For i=1 To n c=Mid(s,i,1)p=Asc(c)+5 co=co+Chr(p)Next i Text2.Text=co End Sub Private Sub command1_click()不得删除本行注释For N=1 To 15 x(N)=Int(Rnd*91+10)Text1.Text=Text1.Text+Str(x(N)Next N End Sub Private Sub Command2_Click()不得删除本行注释Dim m As Integer,K As Integer For N=1 To 15 For m=N+1 To 15 If x(m)x(N)Then K=x(N)x(N)=x(m)x(m)=K End If Next m Text2.Text=Text2.Text+Str(x(N)Next N End Sub名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 12 页 -Private Sub Command1_Click()Dim char As String*1 char=Trim(Text1.Text)If Not IsNumeric(char)Then If(Asc(char)=65 And Asc(char)=97 And Asc(char)=120)Then Label1.Caption=输入的是字母 Else:Label1.Caption=输入的是其它符号 End If Else:Label1.Caption=输入的是数字 End If End Sub Private Sub Command1_Click()Text1.Text=UCase(Text1.Tag)End Sub Private Sub Command2_Click()Text1.Text=LCase(Text1.Tag)End Sub Private Sub Command3_Click()Text1.Text=Text1.Tag End Sub Private Sub Text1_KeyUp(KeyCode As Integer,Shift As Integer)对于文本框控件中输入的每次键盘按键弹起事件做一次文本内容保存Text1.Tag=Text1.Text 用 Text1.Tag 属性作为保存textbox 控件文本内容的临时存储区End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 12 页 -Private Sub command1_Click()不得删除本行注释Dim a As Integer,b As Integer,c As Integer Dim max As Integer,min As Integer a=Val(Text1.Text)b=Val(Text2.Text)c=Val(Text3.Text)max=a+b+Abs(a-b)max=max/2 min=a+b-Abs(a-b)min=min/2 max=max+c+Abs(max-c)max=max/2 min=min+c-Abs(min-c)min=min/2 Text4.Text=max Text5.Text=min End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 12 页 -Private Sub Command1_Click()Dim a As Integer If Not IsNumeric(Text1.Text)Then MsgBox 请输入数字!,48,输入的不是数字 Else a=Val(Text1.Text)If(a Mod 4=0 And a Mod 100 0)Or a Mod 400=0 Then Text2.Text=闰年 Else:Text2.Text=非闰年 End If End if Text1.SetFocus Text1.SelStart=0 Text1.SelLength=Len(Text1.Text)End Sub Private Sub Command1_Click()不得删除本行注释Dim n As Integer,i As Integer n=Val(Text1.Text)If n 0 Or(Not IsNumeric(Text1.Text)Then MsgBox 请输入正整数,48,输入的不是正整数 Open App.Path&jfwj.txt For Output As#1 For i=1 To n Print#1,String(i,*)Next i Close#1 End Sub名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 12 页 -Private Sub Command1_Click()不得删除本行注释Dim i As Integer For i=1 To 200 If i Mod 3=0 And i Mod 7=0 Then Text1.Text=Text1.Text+Str(i)End If Next End Sub Private Sub Command1_Click()不得删除本行注释Dim sum As Single,i As Integer Open App.Path&Jsdata.txt For Input As#1 For i=1 To 15 Input#1,Arr(i)Next i Close#1 If Op1 Then T1.Text=Aaa End If If Op2 Then T1.Text=Bbb End If End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 12 页 -Private Sub Command1_Click()不得删除本行注释Dim i As Integer,n As Integer,s As Integer n=Val(Text1.Text)If n 0 Or(Not IsNumeric(Text1.Text)Then MsgBox 请输入正整数,48,输入的不是正整数 s=0 For i=1 To n If i Mod 3=0 Then s=s+i End If Next i Text2.Text=s End Sub Private Sub Command1_Click()不得删除本行注释Dim a As Integer a=Val(Text1.Text)Text2.Text=zh(a)zh 过程在原本的程序代码中已经有了直接可用End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 12 页 -Private Sub Command1_Click()不得删除本行注释Dim x As Double Dim y As Double x=Val(Text1.Text)If x 10 Then y=x-1 ElseIf x 20 Then y=2*x-1 Else:y=3*x-1 End If Text2.Text=y End Sub Private Sub Command1_Click()不得删除本行注释Text1.Text=Open App.Path&zfwj.txt For Input As#1 For i=1 To 10 Input#1,a(i)Text1.Text=Text1.Text&a(i)Next i Close#1 End Sub Private Sub Command2_Click()不得删除本行注释Dim t As String Text2.Text=For i=1 To 10 t=a(i)a(i)=a(10-i+1)a(10-i+1)=t Text2.Text=Text2.Text&a(i)Next i End Sub名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 12 页 -Private Sub Command1_Click()Dim a As Integer Dim b As Integer a=Val(Text1.Text)b=Val(Text2.Text)Label1.Caption=Int(Rnd()*(b-a+1)+a)Label2.Caption=Int(Rnd()*(b-a+1)+a)Label3.Caption=Int(Rnd()*(b-a+1)+a)End Sub Private Sub Command1_Click()Dim s as string Open App.Path&Bdata.txt For Input As#1 Do While Not EOF(1)Line Input#1,s Text1.Text=Text1.Text+s+Chr(13)+Chr(10)Loop Close#1 End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 12 页 -Private Sub Command1_Click()不得删改本行注释Dim n As Integer n=Val(Text1.Text)If n 999 Then MsgBox 请输入三位正整数,输入出错 Else:Text2.Text=Int(n Mod 10)Text3.Text=Int(n Mod 100)/10)Text4.Text=Int(n/100)End If End Sub Private Sub Command1_Click()产生按钮的代码i=List1.ListIndex For i=0 To 9 List1.AddItem Int(90*Rnd+10)Next i End Sub Private Sub Command3_Click()清除按钮的代码List1.Clear List2.Clear End Sub Private Sub Command2_Click()不得删改本行注释Dim n As Double,j As Integer i=List1.ListIndex For i=List1.ListCount-1 To 0 Step-1 n=Val(List1.List(i)If n Mod 2=1 Then List2.AddItem n List1.RemoveItem i End If Next i End Sub名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 12 页 -Private Sub Command1_Click()”读入数据”按钮代码不得删除本行注释Dim st As String Text1.Text=Open App.Path&yy.txt For Input As#1 While Not EOF(1)Input#1,st Text1.Text=Text1.Text+st+vbCrLf Wend Close#1 End Sub Private Sub Command2_Click()不得删除本行注释Dim x As String,y As String*1,n As Integer,i As Integer,p As Integer x=Trim(Text1.Text)n=Len(x)p=0 For i=1 To n y=UCase(Mid(x,i,1)If y=A And y k Then List1.AddItem n Next n End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 12 页 -