分支结构的参考例题

上传人:kms****20 文档编号:40426765 上传时间:2018-05-26 格式:DOC 页数:9 大小:179KB
返回 下载 相关 举报
分支结构的参考例题_第1页
第1页 / 共9页
分支结构的参考例题_第2页
第2页 / 共9页
分支结构的参考例题_第3页
第3页 / 共9页
分支结构的参考例题_第4页
第4页 / 共9页
分支结构的参考例题_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《分支结构的参考例题》由会员分享,可在线阅读,更多相关《分支结构的参考例题(9页珍藏版)》请在金锄头文库上搜索。

1、1Private Sub CmdCal_Click()Dim a As Integer, b As Integer, c As Integer, s As Single, area As Singlea = Val(Txta.Text)b = Val(Txtb.Text)c = Val(Txtc.Text)If a + b c And a + c b And c + b a Thens = (a + b + c) / 2area = Sqr(s * (s - a) * (s - b) * (s - c)Txtarea = CStr(area)ElseTxtarea = “输入数据无法构成三角形

2、三边“End IfCmdClear.SetFocus End Sub Private Sub CmdClear_Click()Txta.Text = “Txtb.Text = “Txtc.Text = “Txtarea.Text = “Txta.SetFocus End Sub Private Sub CmdEnd_Click()End End SubOption Explicit Private Sub CmdOld_Click()Dim y As Integer, m As Integer, d As Integer, birth As Datey = TxtYear2m = TxtMon

3、thd = TxtDaybirth = DateSerial(y, m, d) 调用系统函数 DateSerial 将年月日数据变成日期型数据返回TxtOld = (Date - birth) 365 & “岁“ End Sub Private Sub TxtDay_Change()If Val(TxtDay) 31 Or Val(TxtDay) 0 Or Val(TxtYear) Mod 100 = 0 And Val(TxtYear) Mod 400 29 ThenMsgBox “出生日输入错误!“ & vbCrLf & “请重新输入!“TxtDay.SelStart = 0TxtDay.

4、SelLength = Len(TxtDay)End IfElseIf Val(TxtDay) 28 ThenMsgBox “出生日输入错误!“ & vbCrLf & “请重新输入!“TxtDay.SelStart = 0TxtDay.SelLength = Len(TxtDay)End IfEnd IfEnd SelectEnd If End Sub Private Sub TxtDay_KeyPress(KeyAscii As Integer)If KeyAscii 12 Or Val(TxtMonth) 8 ThenIf KeyAscii 48 + 9 ThenMsgBox “出生月份输

5、入错误!“ & vbCrLf & “请重新输入!“KeyAscii = 0End IfEnd If End Sub Private Sub TxtYear_Change()If Val(TxtYear) Year(Date) ThenMsgBox “出生年份输入错误!“ & vbCrLf & “请重新输入!“TxtYear.SelStart = 0TxtYear.SelLength = Len(TxtYear)End If End Sub Private Sub TxtYear_KeyPress(KeyAscii As Integer)If KeyAscii = 0 Thenincome =

6、Val(TxtIncome)Select Case incomeCase Is = 5000tax = (income - 400) * 0.05Case Elsetax = (income - 400) * 0.04End SelectTxtTax = taxElse4MsgBox “收入输入错误!请重新输入!“TxtIncome = “TxtIncome.SetFocusEnd If End SubPrivate Sub CmbWeekDay_Click() 编写组合框 CmbWeekDay 的单击事件过程Select Case CmbWeekDay.TextCase “星期日“LblAr

7、rage.Caption = “休息“Case “星期一“, “星期三“LblArrage.Caption = “讲计算机课“Case “星期二“, “星期四“LblArrage.Caption = “讲程序设计课“Case “星期五“LblArrage.Caption = “进修英语“Case “星期六“LblArrage.Caption = “政治学习“End SelectLblArrage.Caption = CmbWeekDay.Text & “的工作安排是:“ & LblArrage.Caption End Sub Private Sub Form_Load()CmbWeekDay.

8、AddItem “星期日“CmbWeekDay.AddItem “星期一“CmbWeekDay.AddItem “星期二“CmbWeekDay.AddItem “星期三“CmbWeekDay.AddItem “星期四“CmbWeekDay.AddItem “星期五“CmbWeekDay.AddItem “星期六“ End Sub实验实验 6.1 已知小麦级别和数量,求小麦收购总价。 (设一级小麦的收购价是 1.3 元/公斤;二级小麦的收购价是 1.1 元/ 公斤;三级小麦的收购价是 0.9 元/公斤;)鼠标停留在三个级别上将提示各自的收购价。分析:首先在文本框中输入收购数量,然后选择级别,并根

9、据级别计算总价,公式为总价=单价*数量。5Private Sub cmdclear_Click()txtnum = “txttotal = “txtnum.SetFocus End Sub Private Sub cmdcal_Click()If OptL1.Value = True Thentxttotal = Str(Val(txtnum) * 1.3)ElseIf OptL2.Value = True Thentxttotal = Str(Val(txtnum) * 1.1)Elsetxttotal = Str(Val(txtnum) * 0.9)End If End Sub Priva

10、te Sub cmdexit_Click()End End Sub 实验实验 6.2 输入一串字符,限制字符串中只含有数字和字母,并将其中的字母和数字分开成为一个字母串和一个数字串。分析:在输入时即对每一个输入的字符进行判断(通常放在文本框的KeyPress事件中),符合条件的字符分类处 理,不符合条件的字符不予显示。Private Sub txtinput_KeyPress(KeyAscii As Integer)If KeyAscii = Asc(“0“) And KeyAscii = Asc(“a“) And KeyAscii = Asc(“A“) And KeyAscii ,欢迎你登录

11、!” ,否则显示“密码错误,请重新输入! ” ; (3)输入 3 次错误密码后,显示“对不起,你没有权限!” ,退出程序。(a) 例6-3参考界面 (b) 帐号为空时的提示信息 (c)正确登录的信息(d)密码错误的提示信息 (e)三次密码错误后的提示分析: 对于登录问题在本章例 6-5 已有介绍,这里增加了输入错误次数的限制,使用全局变量存放输入错误密码的次数,当 次数超出时及时给出错误信息;在程序执行过程中,对用户输入时可能出现的错误进行了估计,并给出不同的错误提 示信息,这是编写代码前必须考虑的。 Dim N As Integer 全局变量 N Private Sub CmdExit_Cl

12、ick()If MsgBox(“确定退出登录么?“, vbOKCancel) = vbOK Then End End Sub Private Sub CmdLogin_Click()If txtname.Text = 3 Then MsgBox “对不起,你没有权限!对不起,你没有权限!“: End 错误次数达 3 次时MsgBox “密码错误,请重新输入!“, vbExclamationtxtpsw.Text = “txtpsw.SetFocusEnd IfElse 帐号为空时MsgBox “请输入帐号!请输入帐号!“, vbExclamationEnd If End Sub 实验实验 6.

13、4 判断是否是闰年。7分析:闰年的条件是:年份能被4整除,但不能被100整除;或能被400整除的整数Private Sub cmdtest_Click()Dim y As Integery = Val(txtyear.Text) If y Mod 4 = 0 And y Mod 100 trousers Thensuit = trousersclothes = clothes - suittrousers = 0Elsesuit = clothestrousers = trousers - suitclothes = 0End IfIf suit = 50 ThenTxtMoney = Cst

14、r(suit * 80 + clothes * 60 + trousers * 45)ElseTxtMoney = Cstr(suit * 90 + clothes * 60 + trousers * 45)End If End Sub 方法方法 2 Private Sub CmdCalMoney_Click()8Dim clothes As Integer, trousers As Integer, Money As Integerclothes 表示上衣数,trousers 表示裤子数clothes = Val(TxtClothes)trousers = Val(TxtTrousers)I

15、f clothes = trousers ThenIf clothes = 50 ThenMoney = clothes * 80ElseMoney = clothes * 90End IfElseIf clothes trousers ThenIf trous = 50 ThenMoney = trousers * 80 + (clothes - trousers) * 60ElseMoney = trousers * 90 + (clothes - trousers) * 60End IfElseIf clothes = 50 ThenMoney = clothes * 80 + (tro

16、users - clothes) * 45ElseMoney = clothes * 90 + (trousers - clothes) * 45End IfEnd IfEnd IfTxtMoney = Cstr(Money) End Sub 实验实验 6-6 随机生成简单的算式。分析: 简单算式中有三个部分需要产生:操作数 1、运算符、操作数 2,为了降低运算的难度,设定操作数 1 是在20,29的 整数,操作数 2 是在1,10的整数,运算符为“” 、 “” 、 “” 、 “” 。算式产生后,由用户输入结果, “判断结果” 按钮执行判断正误,且用户可以点击“重新出题”显示下一个随机算式。为了能执行判断,在产生算式时就应该将正 确

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号