VB语言程序设计课后编程及上机调试参考答案_第1页
VB语言程序设计课后编程及上机调试参考答案_第2页
VB语言程序设计课后编程及上机调试参考答案_第3页
VB语言程序设计课后编程及上机调试参考答案_第4页
VB语言程序设计课后编程及上机调试参考答案_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

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 Comman

2、d2_Click() Txt.Text = "我没有电脑"End SubPrivate Sub Command3_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 *

3、 r 2 Text1.Text = "圆的面积为" & sEnd Subex0204Private Sub Command1_Click() '"最大化"按钮 Form1.WindowState = 2 '最大化End SubPrivate Sub Command2_Click() '"复原"按钮 Form1.WindowState = 0 '复原End SubPrivate Sub Command3_Click() '"最小化"按钮 Form1.WindowStat

4、e = 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 Subex0206Private Sub command1_click() Dim x As String x = Text1.Text Text2.Text = Left(x, 1) + Right(x, 1)End Subex0301Private Sub Form_Load()

5、 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 = x Mod 10 '求个位数 Show y = c * 100 + b * 10 + a '生成倒序数 Print x; "倒序数为:" y '显示倒序数End Subex0302Privat

6、e 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 Subex0303Private Sub Cmd1_Click() Lab1.Visible

7、 = 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 Subex0305Private Sub Form_Load(

8、) 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 = In

9、putBox("输入一个整数(135000)") s = "奇数" If x Mod 2 = 0 Then s = "偶数" MsgBox sEnd Subex0402Private 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(Text

10、3.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 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,

11、 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 = pEnd Subex0404Priva

12、te 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 Form_Load() LabClk.Caption = "0"

13、; 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 SubPrivate Sub

14、 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 Command1.Enable

15、d = 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

16、* k) Next k Print Int(s * 10 + 0.5) / 10End Subex0503Private Sub Form_Load() Show k = 2 s = 0 Do While k < 101 s = s + k * (k - 1) k = k + 2 Loop Print sEnd Subex0504Private Sub Form_Load() Dim k As Integer, a As Integer, b As Integer Dim c As Integer, n As Integer Show n = 0 Print "三位数的水仙花数

17、是:" For k = 100 To 999 a = Int(k / 100) b = Int(k - 100 * a) / 10) c = k Mod 10 If a * a * a + b * b * b + c * c * c = k Then Print k n = n + 1 End If Next k Print "一共有" & n & "个水仙花数"End Subex0505Private Sub Form_Load() Show For k = 1000 To 9999 If k Mod 78 = 0 Then

18、a = Int(k / 1000) b = Int(k / 100) - 10 * a c = Int(k / 10) - 100 * a - 10 * b d = k Mod 10 If a = b And c = d Then Print k End If End If Next kEnd Subex0506Private Sub Command1_Click() Dim s As String, y As String Dim x As String, t As String s = Text1.Text: y = "" For k = 1 To Len(s) x =

19、 Mid(s, k, 1) t = UCase(x) If t >= "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 '真时为选定 Print List1.L

20、ist(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 &

21、; ",C=" & c & ",D=" & d End If Next 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

22、a(k) < min Then min = a(k) pos = k End If Next k Print "最小数:" min, "位置:" posEnd Subex0602Private Sub Form_Load() Dim a As Variant, b As Variant Dim c(8) As Integer a = Array(3, 4, 2, 1, 5, 7, 8, 11, 13) b = Array(10, 6, 12, 9, 13, 8, 8, 1, 16) For k = 0 To 8 c(k) = a(k) + b(k)

23、 Next k Show For k = 0 To 8 Print c(k) Next kEnd Subex0603Private Sub Form_Load() Dim d() As Integer Dim k As Integer, t As Integer, m As Integer Dim h As Integer, n As Integer n = Val(InputBox("请输入数组个数n:") ReDim d(n) Show Print "处理前数据 :" Randomize For k = 1 To n d(k) = Int(101 *

24、 Rnd - 50) Print d(k); Next k Print m = Int(n / 2) For k = 1 To m h = n - k + 1 t = d(h): d(h) = d(k): d(k) = t Next k Print "处理的结果 :" For k = 1 To n Print d(k); Next kEnd Subex0604Private Sub Form_Load() Dim a(8, 8) As Integer, n As Integer Dim s As Long, i As Integer, j As Integer Show R

25、andomize n = 8 For i = 1 To n For j = 1 To n x = Int(90 * Rnd + 10) '产生一个随机数 a(i, j) = x Next j Next i s = 0 For i = 1 To n For j = 1 To n s = s + a(i, j) Next j Next i Print "所有元素之和;" s Print For i = 1 To n s = 0 For j = 1 To n s = s + a(i, j) Next j Print "第" i; "行元素之和

26、;" s Next i Print s = 0 For i = 1 To n s = s + a(i, i) Next i Print "主对角线元素之和;" s Print s = 0 For i = 1 To n For j = 1 To n If i = 1 Or i = n Or j = 1 Or j = n Then s = s + a(i, j) End If Next j Next i Print "所有靠边元素之和;" sEnd Subex0605Option Base 1Private Sub Form_Load() Dim

27、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 '判断

28、有无更大数 max = a(i, j): r = i: c = j End If Next j, 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() Di

29、m a(20) As Integer Randomize a(1) = Int(10 + 90 * Rnd) k = 1 Do While k < 20 x = Int(10 + 90 * Rnd) f = 0 For j = 1 To k If x = a(j) Then f = 1 Exit For End If Next j If f = 0 Then k = k + 1 a(k) = x End If Loop Show For j = 1 To 20 Print a(j) Next jEnd Subex0701Private Sub Form_Click() Dim s As

30、Long s = 0 For k = 1 To 10 s = s + jc(k) Next k Print sEnd SubFunction jc(n) As Long Dim t As Long t = 1 For k = 1 To n t = t * k Next k jc = tEnd Functionex0702Private Sub Form_Load() Dim a As Single, b As Single, c As Single Dim d As Single, m As Single, n As Single Show a = Val(InputBox("输入第

31、1个数") b = Val(InputBox("输入第2个数") c = Val(InputBox("输入第3个数") d = Val(InputBox("输入第4个数") m = max(a, b) '求a、b中的较大数 n = max(c, d) '求c、d中的较大数 Print "最大数为:" & max(m, n) '求m、n中的较大数End SubFunction max(x, y) As Single If x > y Then max = x Else

32、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 Show x = Val(

33、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

34、 IntegerPrivate Sub 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 = &q

35、uot;"End Subex0706APrivate Sub Command1_Click() Form2.Hide Form1.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); '输出当前行左边的空格,分号表示下一输出项紧

36、接输出 For j = 1 To 2 * i - 1 '输出2*i-1个字符 If j <= i Then '按左、右两边指定输出字符 k = j '左边输出的字符 Else k = 2 * i - j '右边输出的字符 End If If k > 9 Then k = k + 7 '字母的ASCII码比数字大7 Print Chr(k + 48); '以字符格式输出 Next j Print '换行 Next iEnd Subex0803Dim t As Single, f As IntegerPrivate Sub Com

37、mand1_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 '设置焦点 Text3.Text = "" Text4.Text = "" f = 0 &#

38、39;第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.Text

39、 = 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 Subex0901Pri

40、vate 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 d(i) = Int(1 + 99 * Rnd) List1.AddItem d(i) Next iE

41、nd SubPrivate Sub Command2_Click() '"保存" Open "MyFile4.txt" For Output As #1 For i = 1 To 20 Write #1, d(i) Next i Close #1End SubPrivate Sub Command3_Click() '"读出" Open "MyFile4.txt" For Input As #1 Do While Not EOF(1) Input #1, x List2.AddItem x Loop

42、 Close #1End SubPrivate Sub Command4_Click() '"结束" EndEnd Subex0903Private Sub Form_Load() Show For k = 1 To 6 fn = ".第7章ex070" & k & ".vbp" If Dir(fn) = "" Then Print Mid(fn, 4) & "未编写!" Else Print Mid(fn, 4) & "已编写!" End If Next kEnd Subex0904Private Sub Form_Load() chedir = "C:Documents and SettingsLinMy Documentsfsodir" If Dir(chedir, 16) = "" Then MkDir chedir End If chefile = "C:Wi

温馨提示

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

评论

0/150

提交评论