2022年2022年计算机二级考试VB上机题级答案 .pdf
一、盘上有一名为“36test11.vbp”的文件。其功能为打印如下图所示的金字塔。请改正该程序并调试。Private Sub Command1_Click() For i = 1 To 9 Print Space(16 - i) For j = 1 To i Print Format(j, 0); Next j If i 1 Then For j = i - 1 To 1 Step -1 Print Format(j, 0) Next j End If Print Next i End SubPrivate Sub Form_Load() Command1.Caption = 开始 End Sub 二、 1、建立一个窗体,单机“生成随机数”按钮,生成n 个【 6,30】闭区间内的随机偶数存放于列表框list1 中。其中, n 的值也是一个【10,20】之间的一个随机数。2、单击“查找完数”按钮,找出list1 中所有完数并显示在picture1 中。如果没有完数,则显示“这批随机数中无完数”。Private sub command1-click() Randomize Dim i as inter,n as integer N = int(rnd*11)+10 List1.clear For i = 1 to n List1.additem int(13*rnd)*2+6 Next i End sub Private sub command2-click() Dim i as integer,j as integer Dim n as integer,x as integer Dim s as integer,st as integer Picture1.cls f = 0 N =list1.listcount For i = 0 to n-1 S=0 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 9 页 - - - - - - - - - N = list1.listcount For i = 0 to n-1 S= 0 St =list1.list(i) + ” =X= val(list1.list(i) For j = 1 to x2 If x mod j = 0 then S= s+j St =st &j & “ +”End if Next j If s = x then F= 1 Picture.print left(st.len(st)-1) End if Next i If f=0 then picture1.print ” 这批随机数中无完数”End sub 一、在磁盘上有一名为“36test21.vbp”的文件。其功能为:单击“开始”按钮后,系统自动产生一个【1,100】之间的随机整数。然后,用户通过对话框反复输入整数进行猜数。如果未猜中,则提示输入数据过大或过小;如果猜中了则显示“你猜中了”。程序最多允许猜数 10 次。超过 10 次则显示“你已经猜了10 次,还没猜中! ”并结束程序。请调试错误。Option explicit Private sub command1-click() Dim count as integer,num as integer,fact as integer Randomize Fact = int(rnd*100) =1) Count = 0 Do Num = val(inputbox( “ 输入所猜的数:” ) If num fact then Label.caption = num & ” 过大 ”Elseif num k -1 then List1.additem k S = s + k End if Next k Issprime = s End function Command2-click() Open “ out.txt ” for output as #1 Print #1,sum Close #1 End sub Command3-click() End End sub 一、功能为单击“生成”按钮时,利用随机函数产生10 个( 10100)之间的随机整数(含10 和 100)并显示在列表框list1 中。然后,单击“计算”按钮,在picture1 中显示能被5或 3 整除的数并计算其累加和。请改正错误调试。Option base 1 Dim x(10)as integer Private sub command1-click() List1.clear For i = 1 to 10 X(i) = int(rnd*91 +10) List1.additem x(i) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 9 页 - - - - - - - - - Next i End sub Private sub command2-click() Dim sum as integer Picture1.cls Sum 1 = 10 If x(i) mod 5 = 0 or x(i) mod 3 = 0 then Picture1.print x(i) Sum = sum + x(i) End if Next i Print “ sum =” ;sum End sub 二、 1、建立一个窗体。任意输入一个字符串并单击“提取”按钮,从text1 中提取包含09数字的字符并构成整数(忽略字符串中的加减号和小数点),然后将这些整数显示在文本框text2 中;2、单击“计算”按钮,计算所提取出的整数的累加和并将结果显示在文本框text3 中。Dim a() as long Private sub command1-cli ck() Dim i as integer,n as integer,f as boolean Dim strt as string,x as string,st as string Strt = text1.text N =1 I =1 Do while i “ 0” and x = ” a” and ch = “ a” and ch n then Prime = true Else Prime = false End if End function Public function numl(by val x as integer) as integer Dim r as integer Numl = 0 Do while x0 R = x mod 10 Numl = numk *10 +r X +x10 Loop End function Private sub command1-click() Dim num as integer,numd as integer,str t as string,bz(1000) as boolean For i = 10 to 1000 Bz(i) = false For num = 10 to 1000 If prime(num) and bz(num) = false then Numd = numl(num) If prime(numd) and (numnumd) then Strt = str(num)& “ -” & str(numd) List1.additem strt Bz(num)= true Bz(numd)= true End if End if Next num End sub 一、有一文件,其功能为当用户单击“开始”按钮时,弹出一个对话框要求用户输入一个311 之间的奇数 (含 3 和 11) ,然后按公式 “1/(3!+(3!+5! )+(3!+5!+7! )+ +(3!+5!+7!+ +n! ) ”计算,并将结果显示在文本框中(注意,此文本框不允许接受键盘输入) 。请改正并调试。Option explicit 省略Dim inta as long ,intb as long,ints as long Dim inti as integer,n as integer Dim vart as variant Do 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 9 页 - - - - - - - - - N = val(inputbox( “ 输入一个( 3-11)之间的奇数:” ) Loop until (n=3 and n =11) and (n mod 2 0) Inta = 1 Intb = 2 Ints = 0 Vart = 0 For inti = 3 to n step 2 Inta = intb *inti Intb = inta*(inti +i) Ints = ints +inta Vart = vart +ints Next inti Vart = 1/vart Text1.text= vart End sub Private sub form-load() Text1.enabled = false End sub 二、建立一个窗体。1、在程序启动时只有“生产”按钮可用。在当单击生成按钮时,产生30 个【 25,85】区间范围内的整数(含25 和 85)并显示在列表框中,同时,“排序”和“存盘”按钮可用且“生成”按钮变为不可用;2、单击“排序”按钮后将30 个整数按从大到小的顺序排序并显示在列表框中;3、单击“存盘”按钮后,将list1 列表框中的数据写入文件“out.txt” 这。Option base 1 Dim a(30) as integer Private sub command1-click List1.clear Randomize For i =1 to 30 A(i) = int(61*rnd)+25 List1.additem a(i) Next i Command1.enabled = false Command2.enabled = true Command3.enabled = true End sub Private sub command2-click() List1.clear For i = 1 to 29 For j = i to 30 If a(i) a(j) then Temp = a(i) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 9 页 - - - - - - - - - A(i)=a(j) A(j)=temp End if Next j List1.additem a(i) Next i End sub Private sub command3-click() Open “ out.txt” for output as #1 For i =- 1 to 30 Print #1,a(i) Next i Close #1 End sub Private sub form-load() Command1,enabled = true Command2,enabled = false Command3,enabled = false End sub 一、其功能为: 单击“计算”按钮,则计算文本框text1 和 text2 中所输入的两个整数的最大公约数并显示在文本框text3 中。请改正并调试。Option explicit Private function gcd(by val x as long,byval y as long) Dim r as integer R =x mod y If r = 0 then Gcd = y Else X =y Y=r Gcd = gcd(x,y) End if End function Command1(省略 ) Dim m as long,n as long Dim gcdvalue as long M = val(text1) N = val(text2) If m 0 and n0 then 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 9 页 - - - - - - - - - Gcdvalue =gcd(m,n) Text3= cstr(gcdvalue) End if End sub 二、建立一个窗体。1、单击“生成”按钮,利用随机函数生成一个43 的整数矩阵,其中每个元素的数据范围为【 100,999】并按图 2 格式显示在图片框picture1 中;2、单击“生成”按钮前“查找”按钮不可用。在单击“生成”后,“查找”按钮可用而“生成”按钮变为不可用。3、单击“查找”按钮后,在已生成的矩阵中查找最大值元素并将其值以及行号和列号显示在文本框text1 中;Option base 1 Dim a(4,3) as integer Private sub command1-click() Randomize For 1 = 1 to 4 For j = 1 to 3 A(i,j) =int(900*rnd)+100 Picture1.print tab(j*5); a(i,j); Next j Picture1.print Next i Command1.enabled=false Command2.enabled = true End sub Command2(省略 ) Dim max as integer,col as integer,cow as integer Max = a(1,1):col =1:cow = 1 For i= 1to 4 For j= 1to 3 If a(i,j) max then Max = a(i,j) Col = i Cow = j End if Next j Text1.text = “ 最大值 :” & max & “行号: ” & col & ”列号: ” & cow Next i End sub Form( 省略 ) Command1.enabled = true Cokmmand2.enabled = false End sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 9 页 - - - - - - - - -