《Visual Basic程序设计教程》
<编程及上机调试>题参考答案
第1章,编程及上机调试》
1,按要求设置窗体属性。
2.在窗体上建立一个标签Label1。程序代码如下:
Private Sub Form_Click()
Label1.Caption = "你单击了窗体"
End Sub
Private Sub Form_DblClick()
Label1.Caption = "你双击了窗体"
End Sub
3,在窗体上建立两个文本框Text1(输入数)、Text2(输出数)和一个命令按钮Command1。两个文本框的Text属性均为空。
Private Sub Command1_Click()
Dim x As Single
x = Val(Text1.Text)
Text2.Text = 15 * x
End Sub
第2章《编程及上机调试》
1~3题:按要求完成操作练习。
4.在窗体上设置三个命令按钮,其名称分别为Command1(“最大化”)、Command2(“还原”)和Command3(“最小化”)。程序代码如下:
Private Sub Command1_Click() '"最大化"按钮
Form1.WindowState = 2 '最大化
End Sub
Private Sub Command2_Click() '"还原"按钮
Form1.WindowState = 0 '还原
End Sub
Private Sub Command3_Click() '"最小化"按钮
Form1.WindowState = 1 '最小化
End Sub
5.在窗体上建立三个文本框Text1(第一个数)、Text2(第二个数)、Text3(计算结果)和一个命令按钮Command1(“计算”)。
Private Sub Command1_Click()
Dim x As Single
Dim y As Single
x = Val(Text1.Text)
y = Val(Text2.Text)
Text3.Text = x * x + y * y
End Sub
6,在窗体上建立两个文本框Text1(输入一串字符)、Text2(输出结果)和一个命令按钮Command1。
Private Sub Command1_Click()
Dim x As String
x = Left(Text1.Text,1) + Right(Text1.Text,1)
Text2.Text = x
End Sub
第3章《编程及上机调试》
1.在窗体上建立一个文本框Text1(输入n值)、三个标签Label1(第一个随机数)、Label2(第二个随机数)和Label3(第三个随机数),以及一个命令按钮Command1。
Private Sub Command1_Click()
n = Val(Text1.Text)
Randomize
x = Int(1 + n * Rnd)
Label1.Caption = "第一个随机数" + Str(x)
x = Int(1 + n * Rnd)
Label2.Caption = "第二个随机数" + Str(x)
x = Int(1 + n * Rnd)
Label3.Caption = "第三个随机数" + Str(x)
End Sub
2.在窗体上建立了四个文本框Text1、Text2、Text3和Text4,以及两个命令按钮Command1(“交换”)和Command2(“合并”)。
Private Sub Command1_Click() '“交换”
t = Text1.Text
Text1.Text = Text2.Text
Text2.Text = Text3.Text
Text3.Text = t
End Sub
Private Sub Command2_Click() '“合并”
t = Text1.Text + Text2.Text + Text3.Text
Text4.Text = t
End Sub
3,(1) Rnd * 9000 + 1000 (2) Str(x) (3) Mid(s,3,1) + Mid(s,2,1) + Left(s,1)
4,Private Sub Cmd1_Click()
Lab1.Visible = True
Lab1.Caption = "您已下达显示命令"
End Sub
Private Sub Form_Click()
Cmd1.Visible = True
End Sub
5,Private Sub Form_KeyPress(KeyAscii As Integer)
Show
Print "输入字符:"; Chr(KeyAscii),"ASCII码为;"; KeyAscii
End Sub
Private Sub Form_DblClick()
Cls
End Sub
6,产生这三个消息框的相应语句是:
产生第1个消息框:x = MsgBox("在这里显示提示信息",4 + 0,"请确认")
产生第2个消息框:x = MsgBox("在这里显示" + Chr(13) + "提示信息",3 + 0,"请确认")
产生第3个消息框:x = MsgBox("在这里显示"+Chr(13)+"提示信息",2 + 16 + 0,"请确认")
第4章《编程及上机调试》
1,在窗体上建立两个文本框Text1(输入数)及Text2(显示结果),一个命令按钮Command1(“判断”)。
Private Sub Command1_Click() '“判断”
x = Val(Text1.Text)
If x Mod 2 = 0 Then
Text2.Text = "偶数"
Else消息
Text2.Text = "奇数"
End If
End Sub
2,在窗体上建立两个文本框Text1、Text2及Text3,分别代表学号、年级及学生类型,建立一个命令按钮Command1(“判断”)。
Private Sub Command1_Click()
Dim t As String,p As String
t = Mid(Text1.Text,5,1)
Select Case t
Case "2"
p = "博士生"
Case "3"
p = "硕士生"
Case "4"
p = "本科生"
Case "5"
p = "专科生"
Case Else
p = "无效学号"
End Select
Text2.Text = Left(Text1.Text,2) + "级"
Text3.Text = p
End Sub
3.窗体上建立了一个文本框Text1(“输入口令”)和一个命令按钮Command1(“检查”)。
Private Sub Command1_Click() '“检查”
x = Text1.Text
If x = "12345678" Then
MsgBox "欢迎你用机!"
Else
MsgBox "对不起,口令错"
End If
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text1.MaxLength = 8
Text1.PasswordChar = "*"
End Sub
4.窗体上建立了两个文本框Text1(输入字符串)及Text2(转换结果),一个命令按钮Command1(“清除”)。
Private Sub Command1_Click() '“清除”
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim s As String * 1
s = Chr(KeyAscii)
Select Case s
Case "A" To "Z"
s = LCase(s)
Case "a" To "z"
s = UCase(s)
Case Else
s = "*"
End Select
Text2.Text = Text2.Text + s
End Sub
5,分别选择”红”,”绿”,”蓝”复选框后,单击命令按钮Command1时,通过RGB函数调配的颜色作为一个标签的背景色(BackColor)。
Private Sub Command1_Click()
r = 0,g = 0,b = 0
If Check1.Value = 1 Then '”红”复选框
r = 255
End If
If Check2.Value = 1 Then '”绿”复选框
g = 255
End If
If Check3.Value = 1 Then '”蓝”复选框
b = 255
End If
Label1.BackColor = RGB(r,g,b)
End Sub
6.程序代码如下:
Private Sub Form_Load()
LabClk.Caption = "0"
LabClk.Alignment = 1
LabClk.BorderStyle = 1
LabClk.FontSize = 22
LabClk.FontName = "黑体"
TimClk.Interval = 1000
End Sub
Private Sub TimClk_Timer()
LabClk.Caption = Val(LabClk.Caption) + 1
End Sub
7,在窗体上建立了两个文本框Text1及Text2,两个命令按钮Command1及Command2(“复制”及“粘贴”)。在文本框Text1中输入文本“复制文本的演示程序”。
Private Sub Command1_Click() '“复制”按钮
Command1.Enabled = True
Command2.Enabled = True
End Sub
Private Sub Command2_Click() '“粘贴”按钮
Text2.Text = Text1.SelText
Command1.Enabled = False
Command2.Enabled = False
End Sub
Private Sub Form_Load()
Command1.Enabled = False
Command2.Enabled = False
End Sub
Private Sub Text1_MouseUp(Button As Integer,Shift As Integer,X As Single,Y As Single)
If Text1.SelLength > 0 Then
Command1.Enabled = True
End If
End Sub
第5章《编程及上机调试》
1,和数为:1.21。程序代码如下:
Private Sub Form_Load()
Show
s = 0
For k = 1 To 100
s = s + 1 / (1 + k * 4)
Next k
Print "s="; Format(s,"##.00")
End Sub
2,程序代码如下:
Private Sub Form_Load()
Dim i As Integer,a As Integer,b As Integer,c As Integer
Show
For i = 100 To 999
a = Int(i / 100)
b = Int((i - 100 * a) / 10)
c = i - 100 * a - 10 * b
If a * a * a + b * b * b + c * c * c = i Then
Print i
End If
Next i
End Sub
3,在窗体上建立一个文本框Text1(初始值)、一个列表框List1(显示结果)和一个命令按钮Comd1(“查找”)。
Private Sub Comd1_Click() '“查找”
Dim n As Integer
n = Val(Text1.Text)
List1.Clear:k = 0
Do While k <= 99
If Not (n Mod 7 = 0) Then
List1.AddItem n
k = k + 1
End If
n = n + 1
Loop
End Sub
4.在窗体上建立一个列表框List1和一个“显示表项”命令按钮Command1。程序代码如下:
Private Sub Command1_click() '"显示表项"按钮单击事件
FontSize = 12
Cls
For i = 0 To List1.ListCount - 1 '逐项判断
If List1.Selected(i) Then '真时为选中
Print List1.List(i)
End If
Next i
End Sub
5.(1) KeyPress (2) Combo1.List(k) (3) Combo1.AddItem
6.程序代码如下:
Private Sub Form_Load()
Show
Dim a As Single,xn0 As Single,xn1 As Single
a = Val(InputBox("请输入一个正数"))
xn1 = a
Do
xn0 = xn1
xn1 = (xn0 + a / xn0) / 2
Loop While Abs(xn0 - xn1) >= 0.00001
Print a; "的平方根为"; xn1
End Sub
第6章《编程及上机调试》
1,程序代码如下:
Private Sub Form_Load()
Dim a(6,6) As Integer
Show
For i = 1 To 6
For j = 1 To 6
Select Case i
Case Is < j
a(i,j) = 1
Case Is > j
a(i,j) = 2
Case Is = j
a(i,j) = 0
End Select
Print a(i,j);
Next j
Print
Next i
For i = 1 To 6
t = a(1,i):a(1,i) = a(6,i):a(6,i) = t
Next i
Print,Print
For i = 1 To 6
For j = 1 To 6
Print a(i,j);
Next j
Print
Next i
End Sub
2,程序代码如下:
Option Base 1
Private Sub Form_Load()
Dim a(8,8) As Integer,n As Integer
Dim max As Integer,r As Integer,c As Integer
Show
Randomize
n = 8
For i = 1 To n
For j = 1 To n
x = Int(900 * Rnd + 100) '产生一个随机数
a(i,j) = x
Print x;
Next j
Print
Next i
max = 0,r = 1,c = 1
Print
For i = 1 To n
For j = 1 To n
If a(i,j) > max Then '判断有无更大数
max = a(i,j),r = i,c = j
End If
Next j,i
Print "最大值为:"; max
Print "位于行:"; r,"列:"; c
End Sub
3.在窗体上建立两个列表框List1(处理前数据)和List2(处理后数据),以及标签Label1。程序代码如下:
Private Sub Form_Load()
Dim a(100) As Integer,b(100) As Integer
Randomize
n = 100
For i = 1 To n '输入数组a的n个元素
x = Int(20 * Rnd)
List1.AddItem x
a(i) = x
Next i
k = 0
For i = 1 To n '删除零元素
If a(i) <> 0 Then
k = k + 1,b(k) = a(i),List2.AddItem b(k)
End If
Next i
Label1.Caption = "数组中零元素个数:" + Str(n - k)
End Sub
4.在窗体WinForm1中建立1个单选按钮组Opt1和1个命令按钮Cmd1,单选按钮组包含3个单选按钮,其标题分别为“单选1”、“单选2”和“单选3”,下标分别为0、1和2。设置第一个单选按钮被选中。程序代码如下:
Dim n As Integer
Private Sub Cmd1_Click()
n = n + 1
If n = 3 Then n = 0
Opt1(n).Value = True
End Sub
5,(1) s(i) < s(j) (2) t = n(i),n(i) = n(j),n(j) = t
(2) s(i) <> s(i-1) (4) h = h + 1
第7章《编程及上机调试》
1,在窗体上建立四个文本框Text1和一个命令按钮Command1(“检验”)。文本框Text1~Text3用于输入,Text4用于输出结果。
Private Sub Command1_Click()
Dim a As String,b As String,c As String
a = Trim(Text1.Text)
b = Trim(Text2.Text)
c = Trim(Text3.Text)
If che(a) And che(b) And che(c) Then
Text4.Text = Val(a) + Val(b) + Val(c)
Else
Text4.Text = "存在非数字字符"
End If
End Sub
Function che(x As String) As Boolean '若存在非数字字符,函数值为假
che = True
For k = 1 To Len(x)
s = Mid(x,k,1)
If s < "0" Or s > "9" Then
che = False
Exit For
End If
Next k
End Function
2.在窗体上建立一个文本框控件数组Text1(0)~Text1(5)、一个文本框Text2和一个命令按钮Command1(“比较”)。文本框控件数组用于输入6 个数,Text2用于输出结果。
Private Sub Command1_Click()
Dim d(5) As Single,m As Single
For k = 0 To 5
d(k) = Val(Text1(k).Text)
Next k
Call max(d(0),d(1),d(2),m)
Call max(m,d(3),d(4),m)
Call max(m,m,d(5),m)
Text2.Text = m
End Sub
'标准模块文件
Sub max(x,y,z,m) '比较三个数x、y、z的大小,并返回大数m
If x > y Then m = x Else m = y
If m < z Then m = z
End Sub
3,(1) s<>"" (2) Mid(s,p+1) (3) List1.ListCount
4,程序代码如下:
Private Sub Form_Load()
Dim x As Single,n As Integer,y As Double
x = InputBox("x=","求X的n次方")
n = InputBox("n=","求X的n次方")
y = Power(x,n)
Show
Print x; "的"; n; "次方="; Format(y,"###,###.##")
End Sub
Function Power(x As Single,n As Integer) As Double
If n > 1 Then
Power = Power(x,n - 1) * x
Else
Power = x
End If
End Function
5,创建一个新工程,建立二个窗体Form1及Form2,把Form2设置为启动窗体。在Form1中设置命令按钮C1,Form2中设置命令按钮C2。
程序代码如下:
Private Sub C1_Click() ‘窗体Form1的“隐藏”按钮
Form1.Hide
End Sub
Private Sub C2_Click() ‘窗体Form2的“显示”按钮
Form1.Show
End Sub
6,在窗体上设置三个窗体Form1~Form3。Form1中建立了两个文本框Text1(“用户名”)和Text2(“密码”),以及命令按钮Command1(“判断”)。Form2中建立了一个文本框Text1(其Text属性值为“欢迎你使用本系统”)和命令按钮Command1(“返回”)。Form3中建立了一个文本框Text1(其Text属性值为“对不起,请向管理员查询”)和命令按钮Command1(“退出”)。
Dim n As Integer
Private Sub Command1_Click() ‘窗体Form1的“判断”
usern = Trim(Text1.Text):passw = Trim(Text2.Text)
If usern = "username" And passw = "password" Then
Form1.Hide
Form2.Show
Else
n = n + 1:Text1.SetFocus
If n = 3 Then
Form1.Hide
Form3.Show
End If
End If
Text1.Text = "":Text2.Text = ""
End Sub
Private Sub Form_Activate()
Text1.Text = "":Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Command1_Click() ‘窗体Form2的“返回”
Form2.Hide
Form1.Show
End Sub
Private Sub Command1_Click() ‘窗体Form3的“退出”
Unload Form1
Unload Form2
Unload Form3
End
End Sub
第8章《编程及上机调试》
1,在窗体上建立一个列表框List1(存放从文件中读出的数据)、一个文本框(存放累加值)和三个命令按钮Command1~Command3。程序代码如下:
Private Sub Command1_Click() '"写入数据"
Dim x As Integer
Open "MyFile3.txt" For Output As #1
For x = 1 To 10
Write #1,x
Next x
Close #1
End Sub
Private Sub Command2_Click() '"读出并累加"
Dim x As Integer,sum As Integer
Open "MyFile3.txt" For Input As #1
List1.Clear
sum = 0
Do While Not EOF(1)
Input #1,x
List1.AddItem x
sum = sum + x
Loop
Close #1
Text1.Text = sum
End Sub
Private Sub Command3_Click() '"结束"
End
End Sub
2,程序代码如下:
Dim d(1 To 20) As Integer
Private Sub Command1_Click() '"产生随机数"
For i = 1 To 20
d(i) = Int(1 + 99 * Rnd)
List1.AddItem d(i)
Next i
End Sub
Private Sub Command2_Click() '"保存"
Open "MyFile3.txt" For Output As #1
For i = 1 To 20
Write #1,d(i)
Next i
Close #1
End Sub
Private Sub Command3_Click() '"读出"
Open "MyFile3.txt" For Input As #1
Do While Not EOF(1)
Input #1,x
List2.AddItem x
Loop
Close #1
End Sub
Private Sub Command4_Click() '"结束"
End
End Sub
3,本程序的用户界面和程序代码较为复杂,请见具体程序文件。
第9章《编程及上机调试》
1,程序代码如下:
Private Sub Form_load()
Dim x(3) As Single,y As Single
On Error GoTo errln '以后出错时转移到errln
Err.Raise 11 '产生"除数为0"的错误
Err.Raise 9 '产生"下标越界"的错误
MsgBox ("终止程序的运行")
End
errln,'标号
Select Case Err.Number
Case 11 '"除数为0"的错误码为11
MsgBox ("除数为0(错误码为11),跳过该错误")
Resume Next '返回
Case 9 '"下标越界"的错误码为9
MsgBox ("下标越界(错误码为9),跳过该错误")
Resume Next '返回
Case Else '其他错误处理
MsgBox ("错误发生在" & Err.Source _
& ",代码为" & Err.Number _
& ",即" & Err.Description)
End Select
End Sub
2.改正后的程序代码如下:
Private Sub Form_Load()
Show
For i = 1 To 11
Print Tab(20 - i);
For j = 1 To 2 * i - 1
If j <= i Then
k = j
Else
k = 2 * i - j
End If
If k > 9 Then k = k + 7
Print Chr(k + 48);
Next j
Next i
End Sub
3.解法一程序如下:
Private Sub Form_Load()
Show
t = 324
For a = 23 To t Step 23
For b = 19 To t Step 19
For c = 43 To t Step 43
If a + b + c = t Then
Print "这三个加数是:"; a,b,c
End If
Next c,b,a
End Sub
解法二程序如下:
Private Sub Form_Load()
Show
t = 324
For a = 23 To t Step 23
For b = 19 To t Step 19
c = t - a - b
If c Mod 43 = 0 And c > 0 Then
Print "这三个加数是:"; a,b,c
End If
Next b,a
End Sub
第10章《编程及上机调试》
1.在窗体上建立一个水平滚动条HScroll1和一个文本框Text1。程序代码如下:
Private Sub Form_Load()
HScroll1.Max = 1000
HScroll1.Min = 0
HScroll1.LargeChange = 5
HScroll1.LargeChange = 10
HScroll1.Value = 500
End Sub
Private Sub HScroll1_Change()
Text1.Text = HScroll1.Value
End Sub
Private Sub HScroll1_Scroll()
Text1.Text = HScroll1.Value
End Sub
2,在窗体上建立一个计时器Timer1和“开始”、“停止”两个命令按钮。程序代码如下:
Private len_S As Single,s As Integer '模块级变量
Private Sub Command1_Click() '“开始”
Timer1.Enabled = True
s = 0
Call Sub_Move
End Sub
Private Sub Command2_Click() '“停止”
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
'先取各指针的长度
len_S = Sqr((LineS.Y2 - LineS.Y1) ^ 2 + (LineS.X2 - LineS.X1) ^ 2) '秒针长度
End Sub
Private Sub Timer1_Timer() '定时器的定时事件过程
s = s + 1
Call Sub_Move
End Sub
Private Sub Sub_Move() '定时器的定时事件过程
pi = 3.14159
LineS.X2 = LineS.X1 + len_S * Sin(pi * s / 30) '绘制秒针
LineS.Y2 = LineS.Y1 - len_S * Cos(pi * s / 30)
End Sub
3,按要求在窗体上设计一个二级下拉菜单。
4,在窗体上建立一个通用对话框CommonDialog1、“打开”及“字体”两个命令按钮和文本框Text1。程序代码如下:
Private Sub Command1_Click() '"打开"
CommonDialog1.DialogTitle = "选择文件"
CommonDialog1.Filter = "文本文件|*.txt" '设置文件过滤器
CommonDialog1.InitDir = "C:\" '设置默认文件夹
CommonDialog1.ShowOpen '显示“打开”对话框
Text1.Text = CommonDialog1.FileName
End Sub
Private Sub Command2_Click() '"字体"
CommonDialog1.Flags = cdlCFScreenFonts
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontName '设置字体名称
Text1.FontSize = CommonDialog1.FontSize '设置字体大小
Text1.FontBold = CommonDialog1.FontBold '粗体
Text1.FontItalic = CommonDialog1.FontItalic '斜体
End Sub
第11章《编程及上机调试》
1,利用可视化数据管理器建立职工人事数据厍(Rsk.mdb),并按表结构格式建立职工表”和“工资表”两个表。对“职工表”按“职工号”字段建立主索引Zgbh,按“姓名”字段建立索引Zgxm;对“工资表”按“职工号”字段建立主索引Gzbh。
2,分别向“职工表”和“工资表”录入一批数据(由读者自行给定),其中“实发数”字段不输入数据。
3,在可视化数据管理器的“SQL语句”窗口中,使用下列SQL语句计算所有职工的实发数:
Update 工资表 Set 实发数 = 基本工资 + 补贴 - 扣除
4,(1),查询1”所用的SQL语句如下:
Select 职工表.职工号,职工表.姓名,职工表.部门
From 职工表
Where (职工表.性别 = '女');
(2),查询2”所用的SQL语句如下:
Select 工资表.职工号,工资表.基本工资,工资表.补贴,工资表.扣除,工资表.实发数
From 工资表
Where (工资表.实发数 > 1500);
(3),查询3”所用的SQL语句如下:
Select 工资表.基本工资,工资表.实发数,职工表.职工号,职工表.姓名,职工表.部门
From 工资表,职工表
Where (工资表.实发数>2000 ) And (职工表.性别='男' ) And 工资表.职工号=职工表.职工号;
5,使用“数据窗体设计器”生成一个名为Zg的数据窗体,用于显示和维护“职工表”中的数据,再把该数据窗体设置为当前工程的启动对象。其操作步骤类似于例11.1。
6,类似于例11.2的操作步骤,在窗体上添加1个数据控件Data1,4个标签Label1~Label4,4个用于显示对应字段的文本框Text1~Text4,1个命令按钮控件数组Comd1(0)~Comd1(4)。设置Data1及4个文本框的属性。编写的程序代码如下:
Private Sub Data1_Reposition()
Data1.Caption = "第" & Data1.Recordset.AbsolutePosition + 1 & "个记录"
End Sub
Private Sub Data1_Validate(Action As Integer,Save As Integer)
If Save = True Then
y = MsgBox("要保存已更改内容吗?",vbYesNo,"保存记录")
If y = vbNo Then '若回答“否”
Save = False
Data1.UpdateControls '恢复原值
End If
End If
End Sub
Private Sub Comd1_Click(Index As Integer)
Select Case Index
Case 0 '查询记录
s = Trim(InputBox("请输入要查找的职工号","查找"))
xh = "职工号='" & s & "'"
Data1.Recordset.FindFirst xh
If Data1.Recordset.NoMatch Then
MsgBox "找不到职工号为" & xh & "的职工 ! "
Data1.Recordset.MoveFirst
End If
Case 1 '添加记录
Data1.Recordset.MoveLast '移到记录集的末尾
Data1.Recordset.AddNew '添加新记录
Case 2 '修改记录
Data1.Recordset.Edit '修改当前记录
Case 3 '删除记录
y = MsgBox("要删除该记录吗? ",vbYesNo,"删除记录")
If y = vbYes Then
Data1.Recordset.Delete '删除记录
Data1.Recordset.MoveNext '显示下一记录
End If
Case 4 '退出
Unload Me
End Select
End Sub
(完)