发布时间 : 星期六 文章计算机二级考试VB上机题级答案更新完毕开始阅读
一、盘上有一名为“36test11.vbp”的文件。其功能为打印如下图所示的金字塔。 请改正该程序并调试。
Private Sub Command1_Click() For i = 1 To 9 Print Space(16 - i) For j = 1 To i
Print Format(j, \ Next j
If i > 1 Then
For j = i - 1 To 1 Step -1 Print Format(j, \ Next j End If Print Next i End Sub
Private 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
N = list1.listcount For i = 0 to n-1 S= 0
St =list1.list(i) +”=’ X= val(list1.list(i)) For j = 1 to x\\2 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 < fact then
Label1.caption = num &”过小” End if
Count = count +1
Loop until num = fact or count = 10 If num = fact then
Label1.caption =num & “你猜中了!共猜了”&count&”次” Else if End sub
Privte sub form-load()
Command1.caption = “开始” End sub
二、建立一个窗体。
1、单击“查找”按钮,按照指定范围找出所有素数并显示在列表框list1中
2、单击“存盘”按钮,计算全部找到素数的累加和并结果存入数据文件“out.txt”。 3、单击“退出”按钮,程序结束。 Dim sum as integer
Function isprime(i as integer ,j as integer) as integer Dim s as integer List1.clear S = 0
For k = i to j
For p = 2 to k -1
If k mod p = 0 then exit for Next p
If p> 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个(10~100)之间的随机整数(含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)
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中提取包含0~9数字的字符并构成整数(忽略字符串中的加减号和小数点),然后将这些整数显示在文本框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 <= len(strt) X = mid(strt,i,1)
If x> “0” and x <=”9” then St = st +x F= false
Redim preserve a(n) A(n) = val(st)
Text2.text = text2.text & a(n)& “ ” St =” “ N = n+1 End if I = i+1 Loop
If f = true then
Redim preserve a(n) A(n) =val(st)
Text2.text = text2.text & a(n) & “ “