




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
3.1交换变量a1、a2的值,P35Private Sub Form_Click()Dim a1 As Integer, a2 As Integer, t As Integera1 = 5: a2 = 10Form1.Print 交换; a1; a2t = a1: a1 = a2: a2 = tForm1.Print 得:; a1; a2End SubPrivate Sub Form_Load()Form1.AutoRedraw = TrueForm1.Print 交换两个数Form1.Print 单击窗体开始End Sub3.2 Tab 函数,P35Private Sub Form_Click()Form1.Print Tab(10); *Form1.Print Tab(11); *Form1.Print Tab(12); *Form1.Print Tab(13); *Form1.Print Tab(14); *End SubPrivate Sub Form_Load()Form1.AutoRedraw = TrueForm1.Print 显示星号三角形,单击窗体开始End Sub3.5 Move 方法在指定位置显示窗体,文本框和图,P45Private Sub Form_Load()Picture1.AutoRedraw = TrueMove 3000, 2000, 2800, 2800Text1.Move 300, 200, 2000, 500Picture1.Move 300, 1000, 1600, 1200Text1.Text = 文本框Picture1.Print 图片框End Sub图3.13 显示和隐藏,P46Private Sub Form_Click()Form1.HideForm2.ShowEnd SubPrivate Sub Form_Load()AutoRedraw = TrueForm1.Print 单击显示form2End SubPrivate Sub Form_dblClick()Form2.HideForm1.ShowEnd SubPrivate Sub Form_Load()AutoRedraw = TrueForm2.Print 双击显示form1End Sub3.6输出位置以及字体设置,P46Private Sub Form_Load()AutoRedraw = TrueForm1.Width = Screen.Width / 2Form1.Height = Screen.Height / 3Form1.Left = (Screen.Width - Form1.Width) / 2Form1.Top = (Screen.Height - Form1.Height) / 2FontName = 黑体: FontSize = 20str1$ = Visual Basic 应用程序CurrentX = (Form1.Width - TextWidth(str1$) / 2CurrentY = (Form1.Height - TextHeight(str1$) / 3Form1.Print str1$End Sub3.12 InputBox及“+”的应用P53Private Sub Form_Click()a = InputBox(enter an integer, 123, 456, , , HelpFile, context)b = InputBox(enter an integer)Text1.Text = b + aEnd Sub3.11计时器,P52Rem “计时开始”按钮单击事件Private Sub Command1_Click()Text1.Text = 计时开始Form1.Tag = TimerCommand1.Enabled = FalseCommand2.Enabled = TrueEnd SubRem “计时停止”按钮单击事件Private Sub Command2_Click()Text1.Text = CInt(10 * (Timer - Form1.Tag) / 10Command1.Enabled = TrueCommand2.Enabled = FalseEnd SubRem“结束”按钮单击事件Private Sub Command3_Click()EndEnd SubRem 窗体装载事件作初始化Private Sub Form_Load()Text1.Text = 计时器Command2.Enabled = FalseTimer1.Interval = 100End SubRem 计时器控制事件 由interval属性设置的时间间隔触发Private Sub Timer1_Timer()Label1.Caption = Time$End Sub3.13 计算平方根,P55Private Sub Form_Click()Dim intFlag As IntegerDim strNumber As StringDim strMSG As StringstrNumber = InputBox(输入一个数)msg$ = 输入的数是: + strNumber + Chr$(13) + Chr(10) + 确认是否正确intFlag = MsgBox(msg$, 36)If intFlag = 6 ThenPrint Sqr(; strNumber; )=; Sqr(Val(strNumber)ElsePrint 取消操作End IfEnd SubPrivate Sub Form_Load()AutoRedraw = TruePrint 计算一个数的平方根Print 单击窗体开始End Sub3.14 Msg函数,P56Private Sub Command1_Click()a$ = software and hardwareb$ = Right(a$, 8)c$ = Mid(a$, 1, 8)MsgBox a$, , b$, c$, 1End Sub4.2 计算对数,P66Private Sub Command1_Click()x = Val(InputBox(输入一个数)If x 0 Then Print Log(x) / Log(10) Else Print 数据错误End SubPrivate Sub Form_Load()Form1.AutoRedraw = tureForm1.Print 计算一个数的常用对数End Sub4.3 字体设置,P68Private Sub Check1_Click()If Check1.Value = 1 ThenText1.FontBold = TrueElseText1.FontBold = FalseEnd IfEnd SubPrivate Sub Check2_Click()If Check2.Value = 1 ThenText1.FontItalic = TrueElseText1.FontItalic = FalseEnd IfEnd SubPrivate Sub Check3_Click()If Check1.Value = 1 ThenText1.FontUnderline = TrueElseText1.FontUnderline = FalseEnd IfEnd SubPrivate Sub Form_Load()Text1.Text = Visual Basic应用程序End SubPrivate Sub Option1_Click()Text1.FontSize = 8End SubPrivate Sub Option2_Click()Text1.FontSize = 12End SubPrivate Sub Option3_Click()Text1.FontSize = 16End Sub4.4 判断闰年,P69Private Sub Command1_Click()x = Val(InputBox(输入年份)If x Mod 400 = 0 Theny$ = 是闰年ElseIf x Mod 4 = 0 ThenIf x Mod 100 0 Theny$ = 是闰年Elsey$ = 不是闰年End IfElsey$ = 不是闰年End IfForm1.Print x; y$End Sub4.5 Case语句,成绩等级判定,P71Private Sub Command1_Click()x = Val(InputBox(输入一个数)Select Case xCase 90 To 100y$ = 优Case 80 To 89y$ = 良Case 70 To 79y$ = 中Case 60 To 69y$ = 及格Case 0 To 59y$ = 不及格Case Elsey = 输入错误End SelectForm1.Print 成绩:; x; Chr$(13) + Chr$(10); 等级:; y$End Sub4.6 计算阶乘,P73Private Sub Command1_Click()n = Val(InputBox(输入一个自然数)term = 1For i = 1 To n Step 1term = term * iNext iForm1.Print n; !=; termEnd Sub4.7 计算“2+4+6+100” ,P73Private Sub Command1_Click()Sum = 0For x = 2 To 100 Step 2Sum = Sum + xNext xForm1.Print Sum=; SumEnd SubPrivate Sub Form_Load()Form1.AutoRedraw = TrueForm1.Print 计算 2+4+6+100End Sub4.8 计算“1+2!+3!+10!” ,P73Private Sub Command1_Click()t = 1: s = 0For k = 1 To 10t = t * ks = s + tNext kForm1.Print sEnd SubPrivate Sub Form_Load()Form1.AutoRedraw = TrueForm1.Print 1+2!+3!+10!=End Sub4.9计算e的近似值,精确到10-6 ,P74Private Sub Command1_Click()Dim e As Single, t As Single, n As Singlee = 1: n = 0: t = 1Don = n + 1t = t / ne = e + tLoop While t 0.000001Print e, nEnd Sub4.11 While - Wend循环,成绩等级划分,P76Private Sub Command1_Click()no = 0: a = 0: b = 0: c = o: d = 0: e = 0x = Val(InputBox(输入成绩,输入0-100以外的数时结束)While x = 0 And x = 90 Thena = a + 1ElseIf x = 80 Thenb = b + 1ElseIf x = 70 Thenc = c + 1ElseIf x = 60 Thend = d + 1Elsee = e + 1End Ifx = Val(InputBox(输入成绩,输入0-100以外的数时结束)WendForm1.Print 总人数:; noForm1.Print 优秀:; aForm1.Print 良好:; bForm1.Print 中等:; cForm1.Print 及格:; dForm1.Print 不及格:; eEnd Sub4.10 猜数字,Do-Loop循环,P75Private Sub Command1_Click()Dim n As Integer, x As IntegerRandomizen = Int(100 * Rnd)Dox = Val(InputBox(输入一个100以内的整数)If x = n ThenForm1.Print x; 正确Exit DoElseIf x n ThenForm1.Print x; 太大ElseForm1.Print x; 太小End IfLoopEnd Sub4.14 组合框,P80Private Sub Combo1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 ThenFor i = 0 To Combo1.ListCount - 1If Combo1.Text = Combo1.List(i) ThenLabel2.Caption = 输入项已在组合中Exit SubEnd IfNext iLabel2.Caption = 已成功添加输入项Combo1.AddItem Combo1.TextEnd IfEnd Sub点菜,列表框Option ExplicitPrivate Sub command1_Click()Dim i%For i = List1.ListCount - 1 To 0 Step -1If List1.Selected(i) ThenList2.AddItem List1.List(i)List1.RemoveItem iEnd IfNext iEnd SubPrivate Sub Command2_Click()Dim i%For i = List2.ListCount - 1 To 0 Step -1If List2.Selected(i) ThenList1.AddItem List2.List(i)List2.RemoveItem iEnd IfNext iEnd SubPrivate Sub Command3_Click()EndEnd SubPrivate Sub Form_Load()List1.AddItem 1List1.AddItem 2List1.AddItem 3List1.AddItem 4List1.AddItem 5List1.AddItem 6List1.AddItem 7List1.AddItem 8End SubPrivate Sub List1_DblClick()List2.AddItem List1.List(List1.ListIndex)List1.RemoveItem List1.ListIndexEnd SubPrivate Sub List2_DblClick()List1.AddItem List2.List(List2.ListIndex)List2.RemoveItem List2.ListIndexEnd Sub4.15 三重For 循环练习,P81Private Sub Command1_Click()Dim x As Integer, n As IntegerDim a As Integer, b As Integer, c As Integern = 0For a = 1 To 3For b = 1 To 3For c = 1 To 3x = 100 * a + 10 * b + cn = n + 1Form1.Print x;If n Mod 10 = 0 Then PrintNext cNext bNext aForm1.PrintForm1.Print 三位数的个数是; nEnd Sub4.16 多重Do - Loop循环及其退出,P82Private Sub Form_Click()Dim cheek, countercheek = Truecounter = 0DoDo While counter 20counter = counter + 1If counter = 10 Thencheek = FalseExit DoEnd IfLoopLoop Until cheek = FalsePrint counter, cheekEnd Sub4.17 有条件转移控制语句,P83Private Sub Command1_Click()Dim a As Integer, b As Integer, c As IntegerRandomize100a = Int(10 * Rnd): b = Int(10 * Rnd)Form1.Print a; +; b; = ;str1$ = Str$(a) + + + Str$(b) + =c = Val(InputBox(str1$)If c = a + b ThenForm1.Print c; 正确ElseForm1.Print c; 错误,重做一题GoTo 100End IfEnd Sub4.18 多分支选择转移,P84Private Sub Command1_Click()Dim n As Integer, b As Singlen = Val(InputBox(输入商品号码14)On n GoTo 110, 210, 310, 410End110 a$ = 圆珠笔b = 2c$ = 上海GoTo 30210 a$ = 钢笔b = 8.5c$ = 成都GoTo 30310 a$ = 日记本b = 3.6c$ = 南京GoTo 30410 a$ = 蓝墨水b = 1.8c$ = 重庆GoTo 3030 Print a$, b, c$End Sub4.19 平方根,GoTo语句控制循环,P85Private Sub Command1_Click()Dim x As Single, y As Singlest:x = Val(InputBox(输入一个数(输入负数结束))If x 0.00001x = (a + b) / 2f1 = a 3 + 4 * a 2 - 10f = x 3 + 4 * x 2 - 10If f1 * f 0 Thena = xElseb = xEnd IfWendPrint x=; CInt(x * 10000) / 10000End Sub4.21百钱白鸡,P87Private Sub Command1_Click()Dim x As Integer, y As Integer, z As IntegerForm1.Print 公鸡, 母鸡, 小鸡For x = 0 To 20For y = 0 To 33z = 100 - x - yIf 5 * x + 3 * y + z / 3 = 100 Then Print x, y, zNext y, xEnd Sub4.22 最大公约数,最小公倍数,P88Private Sub Command1_Click()Dim m As Integer, n As Integer, u As Integer, v As Integerm = Val(Text1.Text)n = Val(Text2.Text)If m n Thenu = m: v = nElsev = m: u = nEnd IfDor = u Mod vu = v: v = rLoop Until r = 0Print 最大公约数:; uPrint 最小公倍数:; m * n / uEnd Sub4.23判断质数,P88Private Sub Command1_Click()Dim n As Integer, i As Integern = Val(InputBox(输入一个大于1的正整数)For i = 2 To n - 1If n Mod i = 0 Then Exit ForNext iIf i n - 1 ThenPrint n; 是质数ElsePrint n; 不是质数End IfEnd Sub5.1 一位数组元素的赋初值,P99Option Base 1Private Sub Form_Click()Dim a As Variant, b As Varianta = Array(1, 2, 3, 4, 5, 6, 7)b = Array(one, two, three)For i = 1 To 7Print a(i);Next iPrintFor i = 1 To UBound(b)Print b(i); ;Next iPrintEnd Sub5.2 一维数组的输入和输出,P100Option Base 1Private Sub Form_Click()Dim a(10) As IntegerFor i = 1 To 10a(i) = Val(InputBox(Enter data)Next iFor i = 1 To 10Print a(i); ;Next iPrintEnd Sub5.4 Split和JoinDim a() As StringPrivate Sub Command1_Click()a = Split(Text1.Text, ,)End SubPrivate Sub Command2_Click()Text2.Text = Join(a, )End Sub5.3 二维数组的输入和输出,P100Option Base 1Private Sub Form_Click()Dim a(3, 3) As IntegerFor i = 1 To 3For j = 1 To 3a(i, j) = Val(InputBox(enter data)Next jNext iFor i = 1 To 3For j = 1 To 3Print a(i, j); ;Next jPrintNext iEnd Sub图5.4 数组的赋值,P102Option Base 1Private Sub Form_click()Dim a As Variant, b() As Varianta = Array(1, 2, 3, 4, 5)b = aPrintPrint 输出b数组:For i = 1 To 5Print b(i); ;Next iEnd Sub5.5 For Each Next 语句输出数组元素的值,P102Option Base 1Private Sub Form_Click()Dim a(10) As IntegerFor i = 1 To 10a(i) = Int(100 * Rnd)Next iPrintFor Each x In aPrint x; ;Next xEnd Sub5.6控件数组的使用,P105Private Sub Option1_Click(Index As Integer)Dim X As Integer, Y As IntegerX = Val(Text1.Text)Y = Val(Text2.Text)Select Case IndexCase 0Label4.Caption = X + YCase 1Label4.Caption = X - YCase 2Label4.Caption = X * YCase 3Label4.Caption = X / YCase 4Label4.Caption = X YCase 5Label4.Caption = X Mod YEnd SelectEnd Sub5.9 求10个数中的最大值和最小值,P110Option Base 1Private Sub Form_Click()Dim a As Varianta = Array(56, 90, -123, 58, 5, 10, 90, 54, 66, 518)Max = a(1)Min = a(1)For i = 2 To 10If a(i) Max Then Max = a(i)If a(i) Min Then Min = a(i)Next iPrintPrint 10个数为:For i = 1 To UBound(a)Print a(i); ;Next iPrintPrint 最大值为:; MaxPrint 最小值为:; MinEnd Sub5.7 自定义数据类型变量,P107Private Type studentnum As String * 8name As String * 8total As Integeraverage As IntegerEnd TypePrivate Sub command1_Click()Dim stud As studentstud.num = 20032001 = 张大山stud.total = 356stud.average = 89Text1(0) = stud.numText1(1) = Text1(2) = stud.totalText1(3) = stud.averageEnd Sub5.7改Private Type studentnum As String * 8name As String * 8total As Integeraverage As IntegerEnd TypePrivate Sub command1_Click()Dim stud As studentWith stud.num = 20032001.name = 张大山.total = 356.average = 89Text1(0) = .numText1(1) = .nameText1(2) = .totalText1(3) = .averageEnd WithEnd Sub5.8 自定义数据类型数组的声明和使用,P108Private Type studentno As String * 6name As String * 6s1 As Integers2 As Integers3 As Integertotal As Singleaver As SingleEnd TypePrivate Sub Form_click()Dim a(1 To 5) As student, t As studentn = 5For i = 1 To na(i).no = InputBox(输入第 & i & 个学生的学号:)a(i).name = InputBox(输入第 & i & 个学生的姓名:)a(i).s1 = Val(InputBox(输入第 & i & 个学生的语文成绩:)a(i).s2 = Val(InputBox(输入第 & i & 个学生的数学成绩:)a(i).s3 = Val(InputBox(输入第 & i & 个学生的英语成绩:)Next iFor i = 1 To na(i).total = a(i).s1 + a(i).s2 + a(i).s3a(i).aver = a(i).total / 3Next iFor i = 1 To n - 1For j = i + 1 To nIf a(i).aver a(j) Thent = a(i)a(i) = a(j)a(j) = tEnd IfNext jNext iPrint 排序后的10个数为:For i = 1 To nPrint a(i); ;Next iEnd Sub5.11 选择法,排序,P112Option Base 1Private Sub Form_Click()Dim a As Varianta = Array(56, 90, -123, 58, 5, 10, 100, 54, 66, 518)n = UBound(a)Print 排序前10个数为:For Each x In aPrint x; ;Next xPrintFor i = 1 To n - 1p = iFor j = i + 1 To nIf a(p) a(j) Then p = jNext jt = a(i)a(i) = a(p)a(p) = tNext iPrint 排序后的10个数为:For i = 1 To nPrint a(i); ;Next iEnd Sub5.12 冒泡法,排序,P113Option Base 1Private Sub Form_click()Dim a As Varianta = Array(56, 90, -123, 58, 5, 10, 100, 54, 66, 518)n = UBound(a)Print 排序前10个数为:For Each x In aPrint x; ;Next xPrintFor i = 1 To n - 1For j = 1 To n - 1If a(j) a(j + 1) Thent = a(j)a(j) = a(j + 1)a(j + 1) = tEnd IfNext jNext iPrint 排序后的10个数为:For i = 1 To nPrint a(i); ;Next iEnd Sub5.13 城市运动会,P114Private Sub Form_Load()Dim nam(1 To 20) As String * 10, t As String * 10For i = 1 To 20nam(i) = InputBox(输入参赛城市名)List1.AddItem nam(i)Next iFor i = 1 To 19For j = i + 1 To 20If nam(i) nam(j) Thent = nam(i)nam(i) = nam(j)nam(j) = tEnd IfNext jNext iFor i = 1 To 20List2.AddItem nam(i)Next iEnd Sub5.14 顺序查询,P114Option Base 1Private Sub Form_Click()Dim a As Varianta = Array(-123, -10, 5, 8, 30, 56, 59, 66, 90, 518)n = UBound(a)Print 数组a中的10个数为:For Each x In aPrint x; ;Next xPrintk = Val(InputBox(输入要查询的关键值)Print 要查询的数是:; kp = 1While k a(p) And p np = p + 1WendIf k = a(p) ThenPrint k; has be found, on; p; th position.ElsePrint k; has not be found.End IfEnd Sub5.15 折半查询,P116Option Base 1Private Sub Form_Click()Dim a As Varianta = Array(-123, -10, 5, 8, 30, 56, 59, 66, 90, 518)n = UBound(a)Print 数组a中的10个数为:For Each x In aPrint x; ;Next xPrintk = Val(InputBox(输入要查询的关键值)Print 要查询的数是:; klow
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 济南市2025-2026学年九年级下学期语文期中测试试卷
- 集安市2025-2026学年八年级下学期语文月考测试试卷
- 电路基础电气知识培训课件
- 电路原理第五章课件
- 电视后期制作知识培训课件
- 高血压课件教学课件
- 高血压病护理课件
- 电脑知识培训讲稿课件
- 电脑知识培训总结课件
- 高考新闻报道压缩课件
- 新版处方管理办法解读
- 《社交媒体的传播》课件
- 利用数字化工具改善医共体慢病管理水平
- 氧化铝工艺流程图解析
- 北京银行招聘考试真题2024
- 2025医院医保培训
- 医院法律法规专题培训课件
- 2025年新闻记者职业资格题库带分析
- 2025-2030中国电气火灾监控模块竞争战略规划与需求预测分析研究报告
- 构建企业ESG与可持续发展计划
- 枞阳县公共停车场智慧停车项目实施方案
评论
0/150
提交评论