《Visual Basic程序设计基础》教材习题参考答案
- 格式:doc
- 大小:160.50 KB
- 文档页数:7
第6章图形控件和图形方法一、判断题1.√2.×3.√4.×5.√6.×7.×8.×9.√10.√二、选择题1.C 2.B 3.C 4.A 5.A 6.C 7.A 8.C 9.C 10.B 11.A 12.B 13.B 14.C 15.C三、填空题1.Circle (ScaleLeft + ScaleWidth / 2, ScaleTop + ScaleHeight / 2), 8002.LoadPicture 3.AutoSize、Stretch、False、False 4.选中、属性5.形状、矩形6.Picture1.Picture=LodePicture("C:\Windows\Cloud.bmp")7.图片框、其他控件8.缇、SclaeMode 9.颜色10.颜色、圆弧起点处转角、圆弧终点处转角、椭圆纵轴与横轴长度之比四、程序阅读题程序1. 转动一条红色直线,其轨迹形成一个圆程序2. 在窗体上随机的位置、用随机的颜色、半径绘制1000个空心的圆。
程序3. 在图片框内绘制多个黄色边框矩形,填充样式在“实心”、“透明”间交替变换。
五、程序填空题1.(1)Combo1.ListIndex (2)Shape1.BorderStyle (3)Combo2.AddItem Str(i) 2.(1)Call pic (2)False (3)n = n + 1 (4)LoadPicture("c:\windows\1.bmp") (4)LoadPicture(fn)六、程序设计题程序1.界面设计略,过程设计如下:Private Sub Form_Load()Picture1.ScaleMode = 3End SubPrivate Sub Command1_Click()Dim x1 As Single, y1 As Single, x2 As Single, y2 As Singlex1 = InputBox("x1="): y1 = InputBox("y1=")x2 = InputBox("x2="): y2 = InputBox("y2=")Picture1.Line (x1, y1)-(x2, y2), , BEnd Sub程序2.界面设计略,过程设计如下:Private Sub Form_click()Dim r As Single, xo As Single, yo As SingleIf Form1.ScaleHeight < Form1.ScaleWidth Thenr = Form1.ScaleHeight / 3Elser = Form1.ScaleWidth / 3End Ifxo = Form1.ScaleLeft + Form1.ScaleWidth / 2yo = Form1.ScaleTop + Form1.ScaleHeight / 2Form1.FillStyle = 0: Form1.FillColor = vbBlueForm1.DrawWidth = 2: Form1.ScaleMode = 6Form1.Circle (xo, yo), r, vbYellowEnd Sub程序3.界面设计略,过程设计如下:Private Sub Form_Load()Dim i As IntegerFor i = 1 To 7: List1.AddItem i: Next iForm1.ScaleMode = 1End SubPrivate Sub List1_Click()Form1.Cls: Form1.ScaleMode = List1.TextForm1.Circle (Form1.ScaleLeft + Form1.ScaleWidth / 2, _ Form1.ScaleTop + Form1.ScaleHeight / 2), 50End Sub。
《Visual Basic程序设计基础》教材习题参考答案第1章 Visual Basic 6.0程序设计概述一、判断题1.√2.√3.×说明:同类控件(如两个标签)相互重叠,设计时可以设置“置前”、“置后”属性。
4.√5.×6.√7.×8.√9.√10.×11.√12.√13.×14.√15.×16.√17.√18.√19.√20.√二、选择题1. C2. B 3.D 4.C 5.B 6.A 7.D 8.A 9.B 10.A 11.C 12.D三、填空题1.对象、事件2.属性、方法、事件3.控件、屏幕(Screen)4.对象5.Left、Top 6.属性、<对象名>.<属性名>=<表达式> 7.网格8.F o rm19.Load或Initialize或Resize或Activate(自动执行的先后顺序也按此排列)10.Activate、Deactivate四、程序设计题程序1.界面设计参看图10-1、10-2以及表10-1。
表10-1 实例2各控件的主要属性设置过程设计如下:Private sub Form_Click()Command2.Enabled = FalseEen SubPrivate Sub Command1_Click()Label2.Visible = FalseLabel1.Caption = Text1.Text + ": 欢迎使用" + Label1.CaptionText1.Visible = FalseCommand1.Enabled = False: Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click()EndEnd Sub程序2.界面设计略,过程设计如下:Private Sub Command1_Click() '字体变大Form1.FontSize = Form1.FontSize + 3Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command2_Click() '字体变小Form1.FontSize = Form1.FontSize - 3Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command3_Click() '字体加粗Form1.FontBold = TrueForm1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command4_Click() '字体不加粗Form1.FontBold = FalseForm1.Cls: Print "欢迎使用VB"End Sub运行时,不得连续、过多次单击“字体变小”按钮,因为当FontSize属性值小于0会产生适时错误。
1.执行下面的程序段后,x 的值为 A 。
x=5For i=1 To 20 Step 2x=x+i\5Next iA.21 B.22 C.23 D.242.在窗体上画两个文本框(其【Name】属性分别为“Text1”和“Text2”)和一个命令按钮(其【Name】属性为“Command1”),然后编写如下事件过程:Private Sub Command1_Click()x=0Do While x<50x=(x+2)*(x+3)n=n+1LoopForm1.Print xForm1.Print nEnd Sub程序运行后,单击命令按钮,窗体上显示的值分别为 B 。
A.1和0 B.2和72 C.3和50 D.4和1683.执行下面的程序段后,s的值为 C 。
s=5For i = 2.6 To 4.9 Step 0.6s=s+1Next iA.7 B.8 C.9 D.104.执行下面的程序段后,s的值为 A 。
s = 0i = 1N=1Do While i < Ns = s + ii = i + 1LoopA.0 B.1 C.2 D.35.执行下面的程序段后,s的值为 B 。
s = 0i = 1N=1Dos = s + ii = i + 1Loop While i < NA.0 B.1 C.2 D.31.在空白处填上代码以完成整个循环。
For i = l To 10Print Tab(16 - i);For j = 1 TO 2 * i - 1Print "*"Next jPrint '换行Next i2.补充代码,以完成查找75最大公约数的功能。
For i=36 To 1 Step -1If 75 mod i == 0 ThenPrint "75最大公约数为",iExit ForEnd ifNext i3.补充代码,以完成查找35和75最大公约数的功能。
VisualBasic程序设计教程第版课后部分编程题参考答案(一)Visual Basic 程序设计教程是程序员必备的一本教材。
而其第四版中也包含了大量课后编程题,能够帮助读者深入理解 Visual Basic 程序设计的核心内容。
本文就来为大家介绍一下这部分编程题的参考答案。
一、数据类型和运算符1. 将两个整数相加Dim num1, num2, result As Integernum1 = 10num2 = 20result = num1 + num2MsgBox ("The result of adding " & num1 & " and " & num2 & " is " & result & ".")2. 将两个小数相减Dim num1, num2, result As Doublenum1 = 2.5num2 = 1.2result = num1 - num2MsgBox ("The result of subtracting " & num2 & " from " & num1 & " is " & result & ".")3. 将两个字符拼接在一起Dim str1, str2, result As Stringstr1 = "Hello"str2 = "World"result = Str1 + Str2MsgBox ("The result of concatenating " & str1 & " and " & str2 & " is " & result & ".")二、程序结构和函数1. 将两个整数相加并返回结果Public Function Add(ByVal num1 As Integer, ByVal num2 As Integer) As IntegerDim result As Integerresult = num1 + num2Return resultEnd Function2. 获取当前日期和时间Dim currentDateTime As StringcurrentDateTime = Now()MsgBox ("The current date and time is " & currentDateTime & ".")3. 显示一个消息框并返回用户的选择Dim userInput As IntegeruserInput = MsgBox("This is a prompt. Would you like tocontinue?", vbYesNoCancel, "Message Box Test")MsgBox ("Your choice was " & userInput & ".")三、数组和结构1. 将一个整数数组按照升序排列Dim numArray() As IntegernumArray = {5, 10, 2, 8, 1}Array.Sort(numArray)MsgBox ("The sorted array is " & Join(numArray, ", ") & ".")2. 创建一个结构体表示一个人,包含姓名、年龄和性别字段Public Structure PersonPublic Name As StringPublic Age As IntegerPublic Gender As StringEnd Structure3. 创建一个数组表示一个班级的学生,使用 Person 结构体表示每个学生的信息Dim students(2) As Personstudents(0).Name = "Tom"students(0).Age = 16students(0).Gender = "Male"students(1).Name = "Jane"students(1).Age = 15students(1).Gender = "Female"students(2).Name = "Bob"students(2).Age = 17students(2).Gender = "Male"四、文件和数据库1. 读取一个文本文件中的内容并显示Dim filePath As String ="C:\Users\UserName\Documents\MyFile.txt"Dim fileText As StringfileText = File.ReadAllText(filePath)MsgBox ("The contents of the file are " & fileText & ".")2. 将一个字符串写入到一个新建的文本文件中Dim filePath As String ="C:\Users\UserName\Documents\NewFile.txt"Dim fileContent As StringfileContent = "This is the content of the new file."File.WriteAllText(filePath, fileContent)MsgBox ("The file was created and the following content was written to it: " & fileContent & ".")3. 从一个 SQL 数据库中查询某个表格的内容Dim connectionString As String = "DataSource=myServerName;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword;"Dim query As String = "SELECT * FROM myTable"Dim dataTable As New DataTable()Using connection As New SqlConnection(connectionString)connection.Open()Using command As New SqlCommand(query, connection)Using adapter As New SqlDataAdapter(command)adapter.Fill(dataTable)End UsingEnd UsingEnd UsingMsgBox ("The following records were retrieved from the database: " & vbNewLine & DataTableToString(dataTable))这些编程题的参考答案可以帮助读者理解 Visual Basic 程序设计的一些基本概念和技术,希望读者能够结合自己的实际情况进行学习和练习,不断提高自己的编程水平。
第1章习题一、选择题1. B2. A3. B4. A二、填空题1. 事件驱动或消息驱动2. 工程资源管理器、窗体三、问答题1. 11个菜单项。
文件菜单包括新建工程、打开工程、添加工程、移除工程、保存工程、工程另存为、保存窗体文件、窗体文件另存为、打印、打印设置、生成可执行文件等命令项;编辑菜单(命令项略,以下同)、视图菜单、工程菜单、格式菜单、调试菜单、运行菜单、查询菜单、图表菜单、工具菜单、外接程序菜单、窗口菜单、帮助菜单。
2. 可以自行定义。
标准工具栏中有添加工程、添加窗体、菜单编辑器、打开工程、保存工程、剪切、复制、粘贴、查找、撤销、恢复、启动、中断、结束、工程资源管理器、属性窗口、窗体布局窗口、对象浏览器、工具箱、数据视图窗口等按钮。
四、编程题(略)第2章习题一、选择题1. A二、练习题1. S1. 把x赋值给同类型变量t;S2. 把y赋值给x;S3. 把t赋值给y。
2. S1. 输入a、b、c;S2. 如果a<b,则交换a与b的数据;S3. 如果a<c,则交换a与c的数据;S4. 如果b<c,则交换b与c的数据;S5. 输出a、b、c。
3. S1. 输入整数N;S2. 给整数K赋值2;S3. 如果K大于N-1,则转S6;S4.如果N除以K 的余数为0,则转S7;S5. K加1再赋值给K,转S3.;S6. 如果K大于N-1,则N为素数;否则N不是素数。
4. S1. 输入整数N;S2. 将N赋值给K;S3. 给整数nx赋值0;S4. 计算nx乘以10加K除以10的余数,再赋值给nx;S5. 计算K除以10的整数商,赋值给K;S6. 如果K>0,则转S4;否则,转S7;S7. 如果N=nx,则N为回文数,否则N不是回文数。
5.6.第3章习题一、选择题1. D2. C3. A4. B5. D6. A7. B8. D9. B 10. D 11. A . 12. C 13. C 14. C二、填空题1. 标题、引用2. ②④⑤3. 64. GotFocus、Resize5. AddItem、Clear6. Label,CommandButton,OptionButton,CheckBox,Frame、TextBox,ListBox,ComboBox7. PictureBox,Frame8. OptionButton,CheckBox,HScrollBar,VScrollBar、OptionButton、CheckBox,HScrollBar,VScrollBar三、问答题1. 答:对象的只读属性的属性值(如名称属性)只能在设计界面时在属性窗口进行设置;非运行属性的属性值则既可在设计界面时在属性窗口进行设置,也可通过程序代码进行设置与修改。
第1章1. 按要求设置窗体属性2.在窗体上建立一个标签Label1。
程序代码如下:Private Sub Form_Click()Label1.Caption = "你单击了窗体"End SubPrivate Sub Form_DblClick()Label1.Caption = "你双击了窗体"End Sub3.在窗体上建立两个文本框Text1(输入数)、Text2(输出数)和一个命令按钮Command1。
两个文本框的Text属性均为空。
Private Sub Command1_Click()Dim x As Singlex = Val(Text1.Text)Text2.Text = 15 * xEnd Sub第2章1~3题:按要求完成操作练习。
4. Private Sub Command1_Click() '"最大化"按钮Form1.WindowState = 2 '最大化End SubPrivate Sub Command2_Click() '"还原"按钮Form1.WindowState = 0 '还原End SubPrivate Sub Command3_Click() '"最小化"按钮Form1.WindowState = 1 '最小化End Sub5.在窗体上建立三个文本框Text1(第一个数)、Text2(第二个数)、Text3(计算结果)和一个命令按钮Command1(“计算”)。
Private Sub Command1_Click()Dim x As SingleDim y As Singlex = Val(Text1.Text)y = Val(Text2.Text)Text3.Text = x * x + y * yEnd Sub6. 在窗体上建立两个文本框Text1(输入一串字符)、Text2(输出结果)和一个命令按钮Command1。
第一章VB概述一.选择题二.填空题1.视图工具箱2.对象事件驱动3.代码窗体设计器4.属性方法事件5.对象的特征6.一个对象可执行的动作7.属性窗口赋值语句三.简答题1.略2.略四.编程题【属性窗口设置】Caption 这是我的第一个练习【代码窗口设置】Private Sub Form_Click()Print "我可以用Visual Basic语言编写程序了!"End Sub第二章窗体与常用控件一.选择题二.填空题1.Value2.下拉列表框3.Scroll4.LargeChange5.ASCII码6.Enabled7.ListCount8.PopupMenu9.Alt10.复选11.CommonDialog1.ShowColor 或者 ShowColor12.Flags13: 114.-15.GFED第三章VB程序设计基础一.选择题二.填空题1.Right(CStr(x) , 1) & Left(CStr(x) , 1)2.x Mod 5=0 Or x Mod 9=03.False4.CDEF5.s >= "A" And s <= "Z" Or s >= "a" And s <= "z"6. x >= 0 And x < 1007.Int(Rnd * (X - 2) + 3)8.Sqr(cos(23 * 3.14 / 180) / (x ^ 2 * Log(y)))9.-4 -5 -410."请输入圆的半径:" "求圆的面积" 211."圆的半径值必须介于1到10之间,请重输" 48 "求圆的面积"第四章VB控制结构一.选择题二.填空题1. 4.8 202. y = 113. 1!+2!+3!+4!+5!4. 9k5. 0n6. 27. x >= 0x > max8. 5 713 1214 119.sum=0sum+jst & k & "+"10.4111011. i + 1List1.RemoveItem j12.Combo1.List(i)=Combo1.Textflag = FalseCombo1.AddItem Combo1.Text 三.编程题1.Private Sub Form_Click()Dim x As Singlex = Val(InputBox("请输入一个x的值:")) If x <= 0 Thenfx = -1ElseIf x < 1 Thenfx = Sqr(x)Elsefx = x ^ 3 + 2End IfMsgBox ("运算结果为:" & fx)End Sub2.Private Sub Form_Click()Dim n As Integer, k As Integer, s As Stringn = Val(InputBox("请输入一个正整数:"))k = 2s = n & "="DoIf n Mod k = 0 Thens = s & k & "*"n = n \ kElsek = k + 1End IfLoop Until n = ks = s & kPrint sEnd Sub3.Private Sub Form_Click()Dim s As String, i As IntegerDim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer s = Text1.TextFor i = 1 To Len(s)If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" ThenIf Val(Mid(s, i, 1)) Mod 2 <> 0 Thena = a + 1Elseb = b + 1End IfElseIf Mid(s, i, 1) >= "A" And Mid(s, i, 1) <= "Z" Thenc = c + 1ElseIf Mid(s, i, 1) >= "a" And Mid(s, i, 1) <= "z" Thend = d + 1Elsee = e + 1End IfNext iPrint "奇数个数为:"; aPrint "偶数个数为:"; bPrint "大写字母个数为:"; cPrint "小写字母个数为:"; dPrint "其他字符个数为:"; eEnd Sub4.Private Sub Form_Click()Dim n As Integer, sum As Integern = Val(InputBox("请输入一个正整数:"))Do Until n = 0sum = sum + n Mod 10n = n \ 10LoopMsgBox ("各位数字之和为:" & sum)End Sub5.Private Sub Form_Click()Dim n As Long, i As Integer, L As Integern = Val(InputBox("请输入一个正整数:"))L = Len(CStr(n))For i = 1 To L / 2If Mid(CStr(n), i, 1) <> Mid(CStr(n), L, 1) Then Exit ForEnd IfL = L - 1Next iIf i > L / 2 ThenMsgBox ("该数是回文数。
第9章数据库与数据访问技术一、判断题1. √2. √3. ×4. √5. ×6. ×7. √8. √9. ×10.×11.√12.√13.√14.√15.√16.×17.×18.×19.√20.√21.√22.×二、选择题1.A2.D3.C4.B5.C6.A7.C8.D9.D 10.C 11.A 12.D 13.A 14.B 15.B 16.C三、程序设计题程序1.(1)数据访问控件以及数据绑定控件的相关属性设置如下Data1:Connect("Access")、DatabaseName("d:\学籍.mdb")、RecordSource("学生)" Text1:DataSource("Data1")、DataField("学号")Text2:DataSource("Data1")、DataField("姓名")Check1:DataSource("Data1")、DataField("性别")Text3:DataSource("Data1")、DataField("出生日期)Text4:DataSource("Data1")、DataField("奖学金")Text5:DataSource("Data1")、DataField("简历")OLE1:DataSource("Data1")、DataField("照片")Adodc1:ConnectionString("Provider = Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info = False") CommandType("1-adCmdText")、RecordSource("select * from 成绩") DataGrid1:DataSource("Adodc1")(2)主要事件过程代码Option ExplicitPrivate Sub Form_Load()Caption = "学籍信息编辑浏览"Label1.Caption = "学号:" : Label2.Caption = "姓名:"Label3.Caption = "出生年月:" : Label4.Caption = "奖学金:"Label5.Caption = "简历:" : Label6.Caption = "照片:"Check1.Caption = "性别(选中表示男,否则为女)"Label7.Caption = "成绩信息查询显示": Adodc1.Visible = FalseEnd SubPrivate Sub Data1_Reposition()Adodc1.RecordSource = "select 成绩.课程号,课程名,成绩from 学生,课程,成绩" _ & "where 学生.学号= 成绩.学号and " & "成绩.课程号= 课程.课程号and " & _ "成绩.学号='" & Data1.Recordset("学号") & "'" & " order by 成绩.课程号" Label7.Caption = Trim(Data1.Recordset("姓名")) & "成绩信息查询显示" Adodc1.RefreshEnd Sub程序2.(1)数据访问控件以及数据绑定控件的相关属性设置如下:Adodc1:ConnectionString("Provider=Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info = False"CommandType("1-adCmdText")、RecordSource("select * from 学生)DataGrid1:DataSource("Adodc1")(2)主要事件过程代码Option ExplicitPrivate Sub Form_Load()Dim i As IntegerCaption = "学籍信息查询浏览": Adodc1.Visible = FalseLabel1.Caption = "选择查询方式:": Combo1.Text = "全部都显示"Label2.Caption = "全部表文件数据浏览表格:" : Label3.Visible = False Text1.Visible = False: Text1.Text = ""For i = 0 To 2Combo1.AddItem Mid("全部都显示按学号查询按姓名查询", 5 * i + 1, 5) Next iEnd SubPrivate Sub Combo1_Click()Select Case Combo1.ListIndexCase 0Text1.Visible = False: Label3.Visible = FalseAdodc1.RecordSource = "select * from 学生"Label2.Caption = "全部表文件数据浏览表格:": Adodc1.Refresh Case 1Label3.Caption = "输入查询条件(学号):" : Text1.Visible = True Label3.Visible = True: Text1.SetFocusCase 2Label3.Caption = "输入查询条件(姓名):" : Text1.Visible = True Label3.Visible = True: Text1.SetFocusEnd SelectEnd SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)If KeyCode = vbKeyReturn ThenIf Combo1.ListIndex = 1 ThenAdodc1.Recordset.MoveFirstAdodc1.Recordset.Find "学号='" & Text1.Text & "'"If Adodc1.Recordset.EOF ThenAdodc1.RecordSource = "select * from 学生"Label2.Caption = "全部表文件数据浏览表格:"Adodc1.Refresh: MsgBox "查无此人!"ElseAdodc1.RecordSource = "select * from 学生 " & "where _学号='" & Text1.Text & "'"Label2.Caption = "学生" + Text1.Text + "的数据浏览表格:"End IfElseAdodc1.Recordset.MoveFirstAdodc1.Recordset.Find "姓名='" & Text1.Text & "'"If Adodc1.Recordset.EOF ThenAdodc1.RecordSource = "select * from 学生"Label2.Caption = "全部表文件数据浏览表格:"Adodc1.Refresh: MsgBox "查无此人!"ElseAdodc1.RecordSource = "select * from 学生 " & _"where 姓名='" & Text1.Text & "'"End IfEnd IfAdodc1.RefreshEnd IfEnd Sub程序3.(1)数据访问控件以及数据绑定控件的相关属性设置如下:Adodc1(xs):ConnectionString("Provider=Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info=False")CommandType("2-adCmdTable")、RecordSource("学生")Adodc2(kc):ConnectionString("Provider=Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info=False")CommandType("2-adCmdTable")、RecordSource("课程")Adodc3(cjcx):ConnectionString("Provider=Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info=False")CommandType("1-adCmdText")RecordSource(" select 成绩.学号,学生.姓名,成绩.课程号,课程.课程名, _ 成绩.成绩 from 学生,课程,成绩 where 成绩.课程号 = 课程.课程号 _and 成绩.学号 = 学生.学号 "DataGrid1:DataSource("cjcx")DataList1:RowSource("xs")、ListField ("姓名")、BoundColumn("学号") DataList2:RowSource("kc")、ListField("课程名")、BoundColumn("课程号")(2)主要事件过程代码Option ExplicitDim p As BytePrivate Sub Form_Load()Dim i As IntegerFor i = 0 To 4Label1(i).Caption = Mid("优秀良好中等及格差等", 2 * i + 1, 2)Text1(i).Text = "": Text1(i).BackColor = RGB(205, 205, 192)Text1(i).Locked = TrueNext icjcx.RecordSource = "select 成绩.学号,学生.姓名,成绩.课程号,课程.课程名, " _ & "成绩.成绩from 学生,课程,成绩 " & _"where 成绩.课程号=课程.课程号 and 成绩.学号=学生.学号"cjcx.Refresh: Label2.Caption = "成绩信息列表"Label3.Caption = "选择学生姓名:" : Label4.Caption = "选择课程名:" xs.Visible = False: kc.Visible = False: cjcx.Visible = False Caption = "学籍信息查询": p = 0End SubPrivate Sub DataList1_Click()p = 0: Call cjdctjEnd SubPrivate Sub DataList2_Click()p = 1: Call cjdctjEnd SubPrivate Sub cjdctj()Dim cjdcjs(5) As Integer, dcase As Byte, i As ByteIf p = 0 Thencjcx.RecordSource = "select 成绩.学号,学生.姓名,成绩.课程号, " _ & "课程.课程名,成绩.成绩 from 学生,课程,成绩 " _& "where 成绩.学号='" & DataList1.BoundText & "' " _& "and (成绩.课程号=课程.课程号 and " _& "成绩.学号=学生.学号) order by 成绩.课程号"Frame1.Caption = "单位:门"Label2.Caption = Trim(DataList1.Text) & "成绩信息列表"Elsecjcx.RecordSource = "select 成绩.学号,学生.姓名,成绩.课程号, " _ & "课程.课程名,成绩.成绩 from 学生,课程,成绩 " _& "where成绩.课程号='" & DataList2.BoundText & "' " _& "and (成绩.课程号=课程.课程号 and " _& "成绩.学号=学生.学号) order by 成绩.学号"Frame1.Caption = "单位:人"Label2.Caption = Trim(DataList2.Text) & "成绩信息列表"End Ifcjcx.RefreshIf Not cjcx.Recordset.EOF Then cjcx.Recordset.MoveFirstWhile Not cjcx.Recordset.EOFIf cjcx.Recordset("成绩") < 60 Thendcase = 0Elsdcase = (cjcx.Recordset("成绩") \ 10) – 5End IfSelect Case dcaseCase 4, 5: cjdcjs(0) = cjdcjs(0) + 1Case 3: cjdcjs(1) = cjdcjs(1) + 1Case 2: cjdcjs(2) = cjdcjs(2) + 1Case 1: cjdcjs(3) = cjdcjs(3) + 1Case 0: cjdcjs(4) = cjdcjs(4) + 1End Selectcjcx.Recordset.MoveNextWendFor i = 0 To 4: Text1(i).Text = cjdcjs(i): Next iEnd Sub程序4.(1)数据访问控件以及数据绑定控件的相关属性设置如下:Adodc1:ConnectionString("Provider = Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info = False")CommandType("2-adCmdTable")、RecordSource("学生")LockType("4-adBatchOptimistic)Adodc2:ConnectionString("Provider = Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info=False")CommandType("2-adCmdTable")、RecordSource("课程")LockType("4-adBatchOptimistic")Adodc3:ConnectionString("Provider = Microsoft.Jet.OLEDB.4.0; _ Data Source = E:\GYH\VB\学籍1b.mdb; Persist Security Info = False")CommandType("2-adCmdTable")、RecordSource("成绩")LockType("4-adBatchOptimistic")Text1:用于显示编辑"学号",但是本字段不设置数据绑定属性,输入或修改后的"学号"数据通过代码方式写入数据源中。
《Visual Basic程序设计基础》教材习题参考答案第1章 Visual Basic 6.0程序设计概述一、判断题1.√2.√3.×说明:同类控件(如两个标签)相互重叠,设计时可以设置“置前”、“置后”属性。
4.√5.×6.√7.×8.√9.√10.×11.√12.√13.×14.√15.×16.√17.√18.√19.√20.√二、选择题1. C2. B 3.D 4.C 5.B 6.A 7.D 8.A 9.B 10.A 11.C 12.D三、填空题1.对象、事件2.属性、方法、事件3.控件、屏幕(Screen)4.对象5.Left、Top 6.属性、<对象名>.<属性名>=<表达式> 7.网格8.F orm19.Load或Initialize或Resize或Activate(自动执行的先后顺序也按此排列)10.Activate、Deactivate四、程序设计题程序1.界面设计参看图10-1、10-2以及表10-1。
表10-1 实例2各控件的主要属性设置过程设计如下:Private sub Form_Click()Command2.Enabled = FalseEen SubPrivate Sub Command1_Click()Label2.Visible = FalseLabel1.Caption = Text1.Text + ": 欢迎使用" + Label1.CaptionText1.Visible = FalseCommand1.Enabled = False: Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click()EndEnd Sub程序2.界面设计略,过程设计如下:Private Sub Command1_Click() '字体变大Form1.FontSize = Form1.FontSize + 3Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command2_Click() '字体变小Form1.FontSize = Form1.FontSize - 3Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command3_Click() '字体加粗Form1.FontBold = TrueForm1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command4_Click() '字体不加粗Form1.FontBold = FalseForm1.Cls: Print "欢迎使用VB"End Sub运行时,不得连续、过多次单击“字体变小”按钮,因为当FontSize属性值小于0会产生适时错误。
程序3.界面设计略,过程设计如下(窗体Height值的计量包括标题、边框,而ScaleHeight 值仅与窗体有效绘图区域的高度有关):Private Sub Form_Load()Text1.Text = "Visual Basic 程序设计"End SubPrivate Sub Form_Resize()Text1.Top = 0: Text1.Left = 0Text1.Width = Form1.ScaleWidth/2 '用 Form1.ScaleWidth效果优于用Form1. WidthText1.Height = Form1.ScaleHeight/2 '用Form1.ScaleHeight优于用Form1. Height Command1.Top = Form1.ScaleHeight - Command1.HeightCommand1.Left = Form1.ScaleWidth - Command1.WidthEnd Sub第2章程序设计基础一、判断题1.√2.×3.×4.×5.√6.×7.√8.√9.√10.×二、选择题1.B 2.D 3.A 4.C 5.C 6.B 7.A 8.D 9. C 10.A 三、填空题1.String 2.4 3."aaa" 4.145 5.6 6.3 7.18 8.False 9.(x Mod 10) * 10 + x \ 10 10.10 + Int( Rnd * 90 ) 11.a * b Mod c12.Log(x)+Sin(3.141593/6) 13.Const PI=3.1415926 14.日期15.Int(x)+1四、程序设计题程序1.界面设计略,过程设计如下:方法一、用文本框控件输入圆的半径Private Sub Command1_Click()Dim x As Singlex = Text1.TextLabel1.Caption = x * x * 3.141593End Sub方法二、用InputBox函数输入圆的半径Private Sub Command1_Click()Dim x As Single '若要求计算结果具有15位有效位数,声明x为Double类型 x = InputBox("r=","输入圆的半径")Label1.Caption = x * x * 3.141593End Sub程序2.略。
第3章结构化程序设计与数组一、判断题1.√2.×3.√4.×5.√6.×7.×8.√9.×10.√二、选择题1.A 2.D 3.C 4.C 5.D 6.B三、填空题1.If x>y Then t=y: y=x: x=t 2.IS3.For i = 0 to 9: B(i) = InputBox("B(" & i & ")="): Next i4.Dim 5.String 6.8四、程序阅读题(写出下列程序的运行结果)程序1. 0 5 程序2. 20 程序3. 2 4 7 11 16程序4. 1 4 9 16 25程序5. 程序6.五、程序填空题1.(1)q * i (2)s + p * i / q (3)Next i2.(1)r = 0 (2)m Mod n3.(1)i + j - 1 (2)7 - i (3)Print4.(1)10 + Int(Rnd * 90) (2)i + 1 To 30 (3)a(i)=a(j) (4)a(i) Mod 2 = 1 六、程序设计题程序1.界面设计略,过程设计如下:Private Sub Form_Click()Dim x As Integer, y As Integer, z As IntegerDim max As Integer, midd As Integer, min As Integerx = InputBox("请输入x的值:")y = InputBox("请输入y的值:")z = InputBox("请输入z的值:")If x > y Then max = x Else max = yIf z > max Then max = zIf x < y Then min = x Else min = yIf z < min Then min = zmidd = x + y + z - max - minPrint max; midd; minEnd Sub程序2.界面设计略,过程设计如下:Private Sub Form_Click()Dim x As Single, y As Singlex = InputBox("请输入x的值:")If x > 3 Theny = x + 3ElseIf x >= 1 Theny = x * xElseIf x > 0 Theny = Sqr(x)Elsey = 0End IfPrint yEnd Sub程序3.界面设计略,过程设计如下:Private Sub Form_Click()Dim i As Integer, j As IntegerFor i = 1 To 9For j = 1 To 9Print Tab((j - 1) * 4); i * j;Next jPrintNext iEnd Sub程序4.界面设计略,过程设计如下:Private Sub Form_Click()Dim x As Single, s As Single, t As SingleDim i As Integer, n As Integerx = InputBox("请输入x的值:")n = InputBox("请输入n的值:")t = x / 2: s = tFor i = 3 To nt = t * x / is = s + tNext iPrint sEnd Sub程序5.界面设计略,过程设计如下:Private Sub Form_Click()Dim e As Single, t As Single, i As Integere = 2: t = 1: i = 1While t >= 0.0001i = i + 1: t = t / i: e = e + tWendPrint eEnd Sub程序6.界面设计略,过程设计如下:Option Base 1Private Sub Form_Click()Dim n As Integer, a(9, 9) As Integer, i As Integer, j As Integer On Error GoTo k '下列代码中若出现错误,转到标号为K的语句执行。
n = InputBox("请输入n,0<n<10")For i = 1 To n: a(i, 1) = 1: a(i, i) = 1: Next iFor i = 3 To nFor j = 2 To i - 1a(i, j) = a(i - 1, j - 1) + a(i - 1, j)Next j, iFor i = 1 To nPrint Tab(30 - 2 * i);For j = 1 To iPrint Space(4 - Len(Trim(Str(a(i, j))))); Trim(Str(a(i, j))); Next jPrintNext iExit Subk: MsgBox "输入n值小于1或大于10,数组下标超界。