VB填空练习答案_第1页
VB填空练习答案_第2页
VB填空练习答案_第3页
VB填空练习答案_第4页
VB填空练习答案_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一.选择结构'-'功能:用输入对话框输入x,根据如图公式计算对应的y,在窗体上输出y的值。'-Private Sub Command1_Click() '*SPACE* Dim x As Single, 【?】 As Single '*SPACE* x = Val(【?】("Please input x :", "输入X的值") If x = 10 Then '*SPACE* 【?】 ElseIf x > 10 Then '*SPACE* y = Sqr(x) 【?】 Else y = 2 *

2、 x 3 + 6 End If Print yEnd Sub (答案) Y InputBox y = 0 + Sin(x)'-'功能:下面的事件过程判断文本框Text1中输入的数所在区间,并在文本框Text2中输出判断结果'-Private Sub Command1_Click() Dim int1 As Integer '*SPACE* 【?】 = Val(Text1.Text) Select Case int1 Case 0 Text2.Text = "值为0" '*SPACE* Case 【?】 Text2.Text = &qu

3、ot;值在1和10之间(包括1和10)" '*SPACE* Case Is > 【?】 Text2.Text = "值大于10" Case Else Text2.Text = "值小于0" End SelectEnd Sub (答案1) int1 1 To 10 10二.一般'-'功能:本程序功能为,第一次单击窗体,Form1的输出结果为 2 5 9'-Private Function sum(n As Integer) '*SPACE* 【?】 j As Integer j = j + n + 1

4、 sum = jEnd FunctionPrivate Sub Form_Click() Dim i%, s% '*SPACE* For 【?】 = 1 To 3 '*SPACE* s = 【?】 Form1.Print s; Next iEnd Sub (答案) Static I sum(i)'-'功能:窗体上建立了一个文本框Text1("输入口令")和一个命令按钮Command1("检查")。' 判断密码是否正确,并显示相应提示' 在加载窗体时设置密码框最大长度为8,密码显示字符为“*”'-di

5、m x as stringPrivate Sub Command1_Click() x = Text1.Text '*SPACE* If x 【?】 "12345678" Then MsgBox ("欢迎你用机!") Else MsgBox ("对不起,口令错") End If Text1.Text = "" Text1.SetFocusEnd SubPrivate Sub Form_Load() Text1.Text = "" '*SPACE* Text1.【?】 = 8 &#

6、39;*SPACE* Text1.【?】 = "*" End Sub (答案) = MaxLength PasswordChar'-'功能:在窗体上画一个标签(名称为 Label1 )和一个计时器(名称为 Timer1 ),然后编写如下几个事件过程:要求:每一秒钟Label1更新显示一次系统时间'-Private Sub Form_Load() Timer1.Enabled = False '*SPACE* Timer1.Interval = 【?】End SubPrivate Sub Form_Click() '*SPACE* Ti

7、mer1.Enabled = 【?】 End SubPrivate Sub Timer1_Timer() '*SPACE* Label1.Caption = 【?】 End Sub (答案) 1000 True Now 或 Time '-'功能:写出程序运行后,单击窗体,Form1上显示的内容' sum 函数的功能为累加求和' isum = 1' isum = 3' isum = 6' isum = 10' isum = 15'-Private Sub Form_Click() Dim I As Integer,

8、isum As Integer '*SPACE* For I = 1 To 【?】 isum = sum(I) '*SPACE* Form1.Print "isum " 【?】 Next IEnd SubPrivate Function sum(n As Integer) Static j As Integer '*SPACE* 【?】 j + n sum = jEnd Function (答案) 5 & isum j =三.级数'-'功能:以下程序段用于求n!'-Private Sub Form_Click() Di

9、m sum As Integer, n As Integer n = InputBox("请输入一个正整数") sum = 0 For i = 1 To n'*SPACE* sum =【?】 Next i Print sumEnd SubPrivate Function mul(ByVal x As Integer) Dim s As Integer, i As Integer s = 1'*SPACE* For i = 1 To 【?】 s = s * i Next i'*SPACE* 【?】End Function (答案) sum + mul(

10、i) x mul = s'-'功能:由输入对话框输入n(n必须为大于零且小于30的自然数), 计算下列表达式的值,并在标签框Label1上显示。 sum=1/2+1/6+ . +1/(k*(k+1) (k<=n)'-Private Sub Form_Click() Dim n As Integer, sum As Double, k As Integer n = Val(InputBox("n=", "请输入自然数n(n>0 且n<30)") '*SPACE* Do While n <= 0 【?】

11、 n >= 30 n = Val(InputBox("n=", "请重输入") Loop sum = 0 k = 0 Do '*SPACE* k =【?】 sum = sum + 1 / (k * (k + 1) '*SPACE* Loop Until 【?】 Label1.Caption = "Sum=" + Str(sum) End Sub (答案) Or k + 1 k >= n四.数字关系'-'功能:过程suixian可以判断某一个数字是否是水仙花数,利用该过程找出三位数中所有水仙花

12、数。' 所谓"水仙花数"是指一个3位数,其各位数字立方和等于该数本身。 '-Private Sub Form_Click()Dim i As IntegerFor i = 100 To 999'*SPACE* 【?】Next iEnd SubPublic Sub suixian(x As Integer)Dim i%, j%, k%'*SPACE*i =【?】j = (x - i * 100) 10k = x - i * 100 - j * 10'*SPACE*If 【?】 Then Print xEnd Sub (答案) call

13、suixian (i) x 100 x = i 3 + j 3 + k 3五.数组'-'功能:下面的程序段,用于实现在一个nXm的矩阵中,找出 值最大的元素所在的行和列,并输出其值及行号和列号。'-Private Sub Form_Click()Dim mat() As IntegerDim n as integer, m As Integern = Val(InputBox("请输入矩阵的行数")m = Val(InputBox("请输入矩阵的列数")'*SPACE*【?】For i = 1 To n For j = 1

14、 To m mat(i, j) = InputBox("请输入数组元素值") mat(i, j) = Val(mat(i, j) Next jNext iPrint "所建立的矩阵为"For i = 1 To n For j = 1 To m Print mat(i, j); Next j PrintNext iMax = mat(1, 1)For i = 1 To n For j = 1 To m'*SPACE* If 【?】 Then Max = mat(i, j) col = j'*SPACE* 【?】 End If Next jN

15、ext iPrintPrint "矩阵最大的元素的值为:" mat(row, col)Print "它所在的行号为:" row; "列号为:" colEnd Sub (答案) ReDim mat(n, m) As Integer mat(i, j) > Max row = I'-'功能:本程序利用二分法查找某数字n是否在已排序的数列当中,' 若在其中则输出其在数列中的位置,否则输出-1。'-Option Base 1Sub birsearch(a(), ByVal low%, ByVal high

16、%, ByVal key, index%) Dim mid As Integer mid = (low + high) 2 If a(mid) = key Then '*SPACE* 【?】 Exit Sub ElseIf low > high Then index = -1 Exit Sub End If If key < a(mid) Then '*SPACE* high = 【?】 Else low = mid + 1 End If '*SPACE* Call birsearch(a(), low, high, 【?】, index)End SubPr

17、ivate Sub Command1_Click() Dim b() As Variant, index As Integer b = Array(5, 13, 19, 21, 37, 56, 64, 75, 80, 88, 92) n = Val(Text1) Call birsearch(b, LBound(b), UBound(b), n, index) Print indexEnd Sub(答案) index = mid mid 1 key'-'功能:单击“插入数据”按钮后,输入一个整型数据,' 该数据会被插入到一个有序的数组a(1 to 10)中,插入该数据后

18、数组还是有序的。'-Option Base 1Private Sub Command1_Click() Dim a%(1 To 10), i%, k%, m% Print "原数列是" For i = 1 To 9 a(i) = (i - 1) * 3 + 1 Print a(i); Next i Print m = Val(InputBox("输入要插入的数据") For k = 1 To 9 '*SPACE* If (m < a(k) Then 【?】 Next k For i = 9 To k Step -1 '*SP

19、ACE* a(【?】) = a(i) Next i '*SPACE* a(k) = 【?】 Print "插入" m; "后的数列是:" For i = 1 To 10 Print a(i); Next i PrintEnd Sub(答案) Exit For i+1 m'-'功能:写出程序运行时单击窗体后,Form1上的输出结果' x(1) = 8' x(2) = 6' x(3) = 4' x(4) = 3' x(5) = 1'-Private Sub Form_Click() Di

20、m x(5) As Integer '*SPACE* Dim i, j, t,【?】 As Integer x(1) = 8 x(2) = 3 x(3) = 1 x(4) = 6 x(5) = 4 For i = 1 To 4 For j = i + 1 To 5 '*SPACE* If x(i) 【?】 x(j) Then t = x(i): x(i) = x(j): x(j) = t Next j, i '*SPACE* For k = 1 To 【?】 Form1.Print "x(" k; ")=" x(k) Next k

21、End Sub(答案) K < 5'-'功能:以下程序段用于输出杨辉三角:结果样式如图1'- Private Sub Form_Click()Const n = 10Dim arr(n, n) As IntegerFor I = 1 To n arr(I, I) = 1'*SPACE* 【?】Next IFor I = 3 To n For j = 2 To I - 1'*SPACE* arr(i, j) =【?】 Next jNext IFor I = 1 To n For j = 1 To I'*SPACE* ? 【?】& &q

22、uot; " Next j PrintNext IEnd Sub(答案) arr(i, 1) = 1 arr(i - 1, j - 1) + arr(i - 1, j) arr(i, j)六.文件'-'功能:本程序执行后,最终在窗体上打印数字7。'-Private Sub Command1_Click() Dim a As String '*SPACE* Open App.Path & "abc.bat" For 【?】 As #1 n = 8 For I = 1 To n Print #1, I + 1 Next I Cl

23、ose #1 '*SPACE* Open App.Path & "abc.bat" For 【?】 As #1 For I = 1 To n Input #1, a If I Mod 5 = 0 Then '*SPACE* Print CInt(a) + 【?】 End If Next I Close #1End Sub(答案) Output Input 1'-'功能:本程序求3100之间的所有素数(质数)并统计个数;' 同时将这些素数从小到大依次写入顺序文件 dataout.txt;' 素数的个数显示在窗体Form1

24、上。'-Private Sub Command1_Click() Dim count As Integer, flag As Boolean Dim t1 As Integer, t2 As Integer '*SPACE* Open App.Path & "dataout.txt" For Output As 【?】 count = 0 For t1 = 3 To 100 '*SPACE* flag = 【?】 For t2 = 2 To Int(Sqr(t1) If t1 Mod t2 = 0 Then flag = False Next

25、 t2 If flag Then '*SPACE* count = 【?】 Write #1, t1 End If Next t1 Form1.Print "素数个数" count Close #1End Sub(答案) #1 True count + 1'-'功能:窗体中有两个List列表,及4个按钮。程序功能,单击“产生随机数”按钮,随机产生20个随机数填入List1中' “保存”按钮 作用是将数组内容写到文件中' “读出”按钮 作用是将文件内容读到数组中' “结束”按钮 作用是结束程序'-Dim d(1 To 2

26、0) As IntegerPrivate Sub Command1_Click() '"产生随机数" Dim i As Integer List1.Clear For i = 1 To 20 '*SPACE* d(i) = Int(1 + 99 *【?】) List1.AddItem d(i) Next iEnd SubPrivate Sub Command2_Click() '"保存" Dim i As Integer Open App.Path & "MyFile3.txt" For Output

27、As #1 For i = 1 To 20 '*SPACE* Write 【?】, d(i) Next i Close #1End SubPrivate Sub Command3_Click() '"读出" Dim x As String Open App.Path & "MyFile3.txt" For Input As #1 List2.Clear '*SPACE* Do While Not 【?】 Input #1, x List2.AddItem x Loop Close #1End SubPrivate Sub

28、Command4_Click() '"结束" EndEnd Sub(答案) Rnd #1 EOF(1)'-'功能: 本程序实现功能:将顺序文本文件 “MYFILE.TXT” 的' 内容一个字符一个字符地读入文本框txtTest中'-Private Sub Form_Click() Dim InputData As String * 1 txtTest.Text = "" '*SPACE* Open App.Path & "MYFILE.TXT" For 【?】 As #1 

29、9;*SPACE* Do While 【?】 EOF(1) ' Do While Not 【?】 Input #1, InputData '*SPACE* txtTest.Text = txtTest.Text + 【?】 Loop Close #1End Sub(答案) Input Not InputData七.杂项'-'功能:程序输出结果为:n=3 x=42 y=9'-Private Sub Command1_Click() Dim n, x, y As Integer '*SPACE* n = 0: x =【?】: y = 0 Do Whi

30、le x < 20 '*SPACE* n = n 【?】 y = x + y x = x * (x + 1) '*SPACE* 【?】 Form1.Print "n=" & Str(n) Form1.Print "x=" & Str(x) Form1.Print "y=" & Str(y)End Sub(答案) 1 + 1 Loop'-'功能:利用下面的过程求m!和m*n'-Private Sub Form_Click()Dim m As Integer, n As

31、 Integer m = 2 n = 3'*SPACE* 【?】End SubPrivate Sub find(x As Integer, y As Integer) Dim s, i As Integer'*SPACE* 【?】 For i = 1 To x s = s * i'*SPACE* p=【?】 Next i Print s, pEnd Sub(答案3) call find(m,n) s = 1 p + y'-'功能:以下程序段用于实现矩阵转置,即将一个n×m的矩阵的行和列互换'-Private Sub Form_Click

32、()Const n = 3Const m = 4Dim a(n, m), b(m, m) As IntegerFor I = 1 To n For j = 1 To m a(I, j) = Int(Rnd * 90) + 10 Next j'*SPACE*【?】For I = 1 To n For j = 1 To m'*SPACE* 【?】 Next jNext IPrint "矩阵转置前"For I = 1 To n For j = 1 To m Print a(I, j); Next j'*SPACE* 【?】Next IPrint "

33、;矩阵转置后"For I = 1 To m For j = 1 To n Print b(I, j); Next j PrintNext IEnd Sub(答案) Next I b(j, i) = a(i, j) Print '功能:下面的程序段用于打印出以下图形'' *' *' *' *' *' *' *''-Private Sub Form_Click()Dim i%, j%, k%For i = 3 To 0 Step -1 For j = 0 To 2 * i Print " &

34、quot; Next j'*SPACE* For k = 5 To 【?】 Print "*" Next k PrintNext iFor i = 0 To 3 For j = 0 To 2 * i + 2'*SPACE* 【?】 Next j For k = 0 To 4 - 2 * i Print "*" Next k'*SPACE* 【?】Next iEnd Sub(答案) 2 * i - 1 Step -1 Print " " Print'-'功能:单击窗体打印内容如图 '-Private Sub Form_Click(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论