1、VB语言程序设计课后编程及上机调试参考答案ex0101Private Sub Label1_Click() Caption = 练习 Command1.Caption = 确定 Label1.FontBold = True Label1.AutoSize = True Label1.Caption = Visual Basic程序设计教程End Subex0102Private Sub Command1_Click() Txt.Text = 我有电脑End SubPrivate Sub Command2_Click() Txt.Text = 我没有电脑End SubPrivate Sub Co
2、mmand3_Click() Txt.Text = 一对双撇号,中间不留空End Subex0104Private Sub Command1_Click() Dim x As Single x = Val(Text1.Text) Text2.Text = 15 * xEnd Subex0202Private Sub Form_Click() Dim r As Single, s As Single r = 4.6 s = 3.14159 * r 2 Text1.Text = 圆的面积为 & sEnd Subex0204Private Sub Command1_Click() 最大化按钮 For
3、m1.WindowState = 2 最大化End SubPrivate Sub Command2_Click() 还原按钮 Form1.WindowState = 0 还原End SubPrivate Sub Command3_Click() 最小化按钮 Form1.WindowState = 1 最小化End Subex0205Private Sub command1_click() Dim x As Single Dim y As Single x = Val(Text1.Text) y = Val(Text2.Text) Text3.Text = x * x + y * yEnd Su
4、bex0206Private Sub command1_click() Dim x As String x = Text1.Text Text2.Text = Left(x, 1) + Right(x, 1)End Subex0301Private Sub Form_Load() Dim x As Integer, y As Integer Dim a As Integer, b As Integer, c As Integer x = Val(InputBox(请输入一个三位数) a = Int(x / 100) 求百位数 b = Int(x / 10) - a * 10 求十位数 c =
5、x Mod 10 求个位数 Show y = c * 100 + b * 10 + a 生成倒序数 Print x; 倒序数为:; y 显示倒序数End Subex0302Private 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 = 第三个随机数 +
6、Str(x)End Subex0303Private Sub Cmd1_Click() Lab1.Visible = True Lab1.Caption = 您已下达显示命令End SubPrivate Sub Form_Click() Cmd1.Visible = TrueEnd Subex0304Private Sub Form_KeyPress(KeyAscii As Integer) Show Print 输入字符:; Chr(KeyAscii), ASCII码为; KeyAsciiEnd SubPrivate Sub Form_DblClick() ClsEnd Subex0305P
7、rivate Sub Form_Load() x = MsgBox(在这里显示提示信息, 4 + 0, 请确认) x = MsgBox(在这里显示 + Chr(13) + 提示信息, 3 + 0, 请确认) x = MsgBox(在这里显示 + Chr(13) + 提示信息, 2 + 16 + 0, 请确认)End Subex0401Private Sub Form_Load() Dim x As Long, s As String x = InputBox(输入一个整数(135000) s = 奇数 If x Mod 2 = 0 Then s = 偶数 MsgBox sEnd Subex04
8、02Private Sub Command1_Click() Dim a As Single, b As Single Dim c As Single, d As Single Dim m As Single, n As Single, x As Single Show a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) d = Val(Text4.Text) If a b Then 求a、b中的较大数 m = a Else m = b End If If c d Then 求c、d中的较大数 n = c Else n = d
9、 End If If m n Then 求m、n中的较大数 x = m Else x = n End If Text5.Text = xEnd Subex0403Private 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
10、) + 级 Text3.Text = pEnd Subex0404Private 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 SubPrivate Sub Command2_Click() EndEnd Subex0405Private Sub For
11、m_Load() LabClk.Caption = 0 LabClk.Alignment = 1 LabClk.BorderStyle = 1 LabClk.FontSize = 22 LabClk.FontName = 黑体 TimClk.Interval = 1000End SubPrivate Sub TimClk_Timer() LabClk.Caption = Val(LabClk.Caption) + 1End Subex0406Private Sub Form_Load() Command1.Enabled = False Command2.Enabled = FalseEnd
12、SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Text1.SelLength 0 Then Command1.Enabled = True End IfEnd SubPrivate Sub Command1_Click() Command1.Enabled = True Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click() Text2.Text = Text1.SelText Comman
13、d1.Enabled = False Command2.Enabled = FalseEnd Subex0501Private Sub Form_Load() Dim k As Integer, s As Integer, t As Integer s = 0 For k = 1 To 100 Step 2 s = s + k Next k Show Print 1100范围内的奇数和:; sEnd Subex0502Private Sub Form_Load() Dim s As Single Show s = 0 For k = 1 To 200 s = s + k / (1 + k *
14、k) Next k Print Int(s * 10 + 0.5) / 10End Subex0503Private Sub Form_Load() Show k = 2 s = 0 Do While k = A And t = Z Then y = y + x End If Next k Text2.Text = yEnd Subex0507Private Sub Command1_click() 显示按钮单击事件 FontSize = 12 Cls For i = 0 To List1.ListCount - 1 逐项判断 If List1.Selected(i) Then 真时为选定 P
15、rint List1.List(i) End If Next iEnd Subex0508Private Sub Form_Load() Show For a = 1 To 9 For b = 1 To 9 For c = 1 To 9 For d = 1 To 9 x = a * 1000 + b * 100 + c * 10 + d y = b * 1000 + a * 100 + a * 10 + c z = d * 100 + d * 10 + a If x - y = z Then Print A= & a & ,B= & b & ,C= & c & ,D= & d End If N
16、ext d Next c Next b Next aEnd Subex0601Private Sub Command1_Click() Dim a(10) As Integer, min As Integer, pos As Integer Randomize For i = 1 To 10 a(i) = Int(Rnd * 90 + 10) Print a(i); Next i Print min = a(1) pos = 1 For k = 2 To 10 If a(k) max Then 判断有无更大数 max = a(i, j): r = i: c = j End If Next j,
17、 i Print 最大值为:; max Print 位于行:; r, 列:; cEnd Subex0606Dim n As IntegerPrivate Sub Cmd1_Click() n = n + 1 If n = 3 Then n = 0 Opt1(n).Value = TrueEnd SubPrivate Sub Form_Load() n = 1End Subex0607Private Sub Form_Load() Dim a(20) As Integer Randomize a(1) = Int(10 + 90 * Rnd) k = 1 Do While k y Then ma
18、x = x Else max = y End IfEnd Functionex0703Private 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 = mEnd Subex0704Private Sub Form_Load() Dim x As Long Sh
19、ow x = Val(InputBox(输入一个数) Print x; 的位数为:; fnws(x)End SubFunction fnws(x As Long) As Integer Dim n As Integer n = 1 Do While x 10 0 n = n + 1 x = x 10 Loop fnws = nEnd Functionex0705Private Sub C1_Click() Form1.HideEnd Subex0705APrivate Sub C2_Click() Form1.ShowEnd Subex0706Dim n As IntegerPrivate S
20、ub Command1_Click() 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 Subex0706APrivate Sub Command1_Click() Form2.Hide Fo
21、rm1.ShowEnd Subex0906BPrivate Sub Command1_Click() Unload Form1 Unload Form2 Unload Form3 EndEnd Subex0802Private Sub Form_Load() Dim i As Integer, j As Integer Show For i = 1 To 12 Print String(20 - i, 32); 输出当前行左边的空格,分号表示下一输出项紧接输出 For j = 1 To 2 * i - 1 输出2*i-1个字符 If j 9 Then k = k + 7 字母的ASCII码比数
22、字大7 Print Chr(k + 48); 以字符格式输出 Next j Print 换行 Next iEnd Subex0803Dim t As Single, f As IntegerPrivate Sub Command1_Click() “产生原稿文”按钮 Randomize s = For k = 1 To 30 x = Chr(Int(Rnd * 26) + 97) 随机产生小写字母 s = s + x Next k Text1.Text = s 显示在文本框中 Text2.Text = Text2.Locked = False 允许录入 Text2.SetFocus 设置焦点
23、Text3.Text = Text4.Text = f = 0 第1次按键标记,0表示未按键End SubPrivate Sub Command2_Click() “结束”按钮 EndEnd SubPrivate Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer) Dim c As Integer, k As Integer If f = 0 Then 第1次按键时,开始计时 t = Timer 用t保存第1次按键的时间 f = 1 1表示已按键和进入录入过程 End If If Len(Text2.Text) 30 Then Text3
24、.Text = Round(Timer - t, 1) & 秒 显示用户当前所用时间 Else c = 0 For k = 1 To 30 统计录入正确的字母个数 If Mid(Text1.Text, k, 1) = Mid(Text2.Text, k, 1) Then c = c + 1 End If Next k Text2.Locked = True 禁止录入 Text4.Text = Round(c / 30 * 100, 2) & % 显示准确率 End IfEnd Subex0901Private Sub Command1_Click() Open 静夜思.txt For Input As #1 Do While Not EOF(1) 文件未结束时,循环 Input #1, x List1.AddItem x Loop Close #1End Subex0902Dim d(1 To 20) As IntegerPrivate Sub Command1_Click() 产生随机数 For i = 1 To 20
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100