【VB开源代码栏目提醒】:网学会员在VB开源代码频道为大家收集整理了“vb程序设计答案 - 编程语言“提供大家参考,希望对大家有所帮助!
2011年程序设计教程详细答案 石河子大学农学院专用 Visual Basic Four short words sum up what has lifted most successful individuals above the crowd: a little bit more. 比别人多一点努力、多一点自律、多一点决心、多一点反省、多一点
学习、多一点实践、多一点疯狂多一点点就能创造奇迹 石河子大学农学院 园艺二班 刘育峰 二○一一年七月一日
Vb程序必须掌握的习题 3-1.设计应用程序输入半径和高求出底面积、侧面积、体积。
Private Sub Form_Click Dim r As Single h As Single Const pi As Single 3.14 r ValText1 h ValText2 Text3 pi a a Text4 2 pi a b Text5 pi a a b End Sub 3.2 单击出题按钮产生任意两个1-100之间的随机整数单击计算按钮求出这两个数的和。
Private Sub Command1_Click Text1 Int100 Rnd 1 Text2 Int100 Rnd 1 End Sub Private Sub Command2_Click Text3 ValText1 ValText2 End Sub 3-3 建立一个标准工程运行时在文本框中输入一个数值型数据单击相应按钮在标签中显示运行结果文本框始终处于选中状态。
Private Sub Command1_Click Label3.Caption SgnText1 End Sub Private Sub Command2_Click Label3.Caption AbsText1 End Sub Private Sub Command3_Click Label3.Caption IntText1 End Sub Private Sub Command4_Click Label3.Caption FixText1 End Sub Private Sub Form_Click Text1.SetFocus Text1.SelStart 0 Text1.SelLength LenText1 End Sub 4.5 用文本框输入一个数判断能否同时被357整除若能整除则用标签输出“能同时被357整除”否则输出“不能整除”。
Private Sub Command1_Click Dim a As Single a ValText1 If a Mod 105 0 Then Label1.Caption quot能同时被3、5、7整除quot Else Label1.Caption quot不能整除quot End If End Sub 4-6 判断输入三条边能否构成三角形若能则计算出面积若不能则给出错误提示并定位焦点重新输入。
Private Sub Form_Click Dim a As Integer b As Integer c As Integer a ValText1 b ValText2 c ValText3 If a b gt c And a - b lt c Then Label1.Caption Sqra b c a b - c a c - b b c - a / 16 Else MsgBox quot数据错误请从新输入quot End If Text1.SetFocus Text1.SelStart 0 Text1.SelLength LenText1 End Sub 4.8 用块结构语句实现。
使用inputbox函数输入3个数并找出3个数中的最大数和最小数。
Private Sub Form_Click Dim a As Integer b As Integer c As Integer a ValInputBoxquot请输入quot b ValInputBoxquot请输入quot c ValInputBoxquot请输入quot If a gt b Then t b b a a t End If If a gt c Then n c c a a n End If If b gt c Then m c c b b m End If Print quot最小值quot a Print quot最大值quot c End Sub 4.9 用select case语句实现使用文本框输入任意一个整数想x根据其除以3的余数判断并输出结果如余数是0则消息框输出strxamp “整除3” 余数是1则输出“除以3余1”余数是2则输出“除以3余2” Private Sub Command1_Click Dim x As Integer b As Integer x ValText1 b x Mod 3 Select Case b Case 0 MsgBox Strx amp quot整除3quot Case 1 MsgBox Strx amp quot除以3余1quot Case 2 MsgBox Strx amp quot除以3余2quot End Select End Sub 4.13 在文本框中输入一个字符则立即判断若是小写字母则把它的大写形式显示在标签中若是大写字母则相反。
若是其它字符则显示原型。
为文本框建立change事件用right函数从文本框中取一个字符。
Private Sub Text1_Change Label1.Caption RightA 1 A Text1.Text If RightA 1 UCaseRightA 1 Then Label1.Caption LCaseRightA 1 ElseIf RightA 1 LCaseRightA 1 Then Label1.Caption UCaseRightA 1 End If End Sub 4.15 按要求编写程序1 form1 窗体的text1文本框初始内容为空初始状态为不可用输入字符时文本框显示字符“”。
2单击form1的“输入密码”按钮则text1变为可用定位焦点。
3输入密码后单击form1的“校正密码”按钮则判断输入的内容是否为小写字母“abc”若是则隐藏form1 窗体显示form2窗体若密码输入错误则提示重新输入三次密码错误则退出
系统。
4单击form2 的返回按钮则隐藏form2窗体显示form1窗体。
Private Sub Command1_Click Text1.Enabled True Text1.SetFocus Text1.SelStart 0 Text1.SelLength LenText1.Text End Sub Private Sub Command2_Click Static times As Integer If Text1.Text LCasequotabcquot Then Form2.Show Form1.Hide Else times times 1 MsgBox quot第quot amp times amp quot次口令错误请重新输入quot: Text1.Text quotquot End If If times 3 Then MsgBox quot3次输入错误请退出quot Text1.Locked True End If End Sub Private Sub Form_Load Text1.Enabled False End Sub Private Sub Command1_Click Form2.Hide Form1.Show End Sub 4.16.利用数列求e的值已知ex1xx2/2x3/3??针对x1求e的近似值最后一项小于10-5. Function factn As Integer As Long If n 1 Then fact 1 Else fact factn - 1 n End If End Function Private Sub Form_Click Dim m As Integer s 1 Do While s gt 10 -5 m m 1 s 1 / factm Sum Sum s Loop Label1.Caption Sum 1 End Sub 4.17 输出100-200之间不能被7整除的数。
Private Sub Form_Click Dim i As Single For i 100 To 200 If i Mod 7 ltgt 0 Then Print i n n 1 If n Mod 10 0 Then Print Next i End Sub 4.18 设计
程序输入n计算s112123????1234??n的值。
Private Sub Form_Click n ValInputBoxquotquot For i 1 To n s s i Sum Sum s Next i Print quotsumquot Sum End Sub 4.19 设s1357????2n1求s不大于400000时的最大值n。
Private Sub Form_Click s 1 Do While s lt 400000 n n 1 m 2 n 1 s s m Loop Label1.Caption n - 1 End Sub 4.20 水仙花数输出100到999之间的所有水仙花数。
Private Sub Form_Click Dim a As Integer b As Integer c As Integer i As Long For i 100 To 999 a Lefti 1 b Midi 2 1 c Righti 1 If i a 3 b 3 c 3 Then Print i End If Next i End Sub 4.21 打出斐波数列的前20项。
Private Sub Form_Click Print quot0quot Dim a As Integer b As Integer c As Integer a 0 b 1 For i 1 To 19 c a b a b b c Print a Next i End Sub 422输入行数在图片框中分别显示出平行四边行正三角倒三角菱形。
Private Sub Command1_Click Cls n ValInputBoxquotnumquot For i 1 To n - 1 Picture.Print Tabn 1 - i Stringn quotquot Next i End Sub Private Sub Command2_Click Cls n ValInputBoxquotnumquot For i 1 To n - 1 Picture1.Print Tabn - i String2 i - 1 quotquot Next i End Sub Private Sub Command3_Click Cls n ValInputBoxquotnumquot For i n - 1 To 1 Step -1 Picture1.Print Tabn - i String2 i - 1 quotquot Next i End Sub Private Sub Command4_Click Cls n ValInputBoxquotnumquot For i 1 To n - 1 Picture1.Print Tabn - i String2 i - 1 quotquot Next i _ For i n - 2 To 1 Step -1 Picture1.Print Tabn - i String2 i - 1 quotquot Next i End Sub 4.23 用带垂直滚动条的文本框输出100到200 之间所有的素数。
1.Private Sub Form_Click Dim i j As Integer For i 100 To 200 b True For j 2 To i - 1 If i Mod j 0 Then b False Exit For End If Next If b True Then Text1.Text Text1.Text amp quot quot amp i Next End Sub 2.Private Sub Form_Click Dim a As Integer b As Integer For i 100 To 200 For j 2 To i - 1 If i Mod j 0 Then Exit For Next j If j gt Sqri Then Text1.Text Text1.Text amp i amp quot quot Next i End Sub 41
设计如图所示单击出题产生任意两个10到50 之间的随机整数于text1和text2中单击求积按钮求积。
Private Sub Command1_Click Text1.Text ValInt40 Rnd 10 Text2.Text ValInt40 Rnd 10 End Sub Private Sub Command2_Click Text3.Text Text1.Text Text2.Text End Sub 4.3程序运行时单击体重指数按钮则先后显示两个输入对话框在两个输入对话框中分别输入身高和体重使输入的值在相应的文本框中显示出来并根据所输出的身高和体重计算体重指数计算结果通过消息框输出。
Private Sub Command1_Click a ValText1.Text b ValText2.Text msg MsgBoxquot你的体重指数为:quot amp vbCrLf amp Roundb / a a 1 0 quotSJT1quot End Sub 4.9 在窗体添加两个标签L1和L2 标题分别为口令和允许次数添加一个命令按钮C1标题为确定再添加两个文本框其中text1用口令输入输入时显示“”无初始内容text2 的初始内容为3请编写程序使得text1 输入口令后单击确定按钮如果输入的是123456则在text1 中显示“口令正确”如果输入其他内容单击确定按钮弹出如图错误提示对话框并且text2中的数字减1最多可输入3次口令若3次都输入错误则禁止再次输入。
Private Sub Command1_Click Static times As Integer If Text1.Text quot123456quot Then Text1.Pass
wordChar quotquot Text1.Text quot口令正确quot Else times times 1 Text2.Text 3 - times MsgBox quot第quot amp times amp quot次口令错误请重新输入quot: Text1.Text quotquot End If If times 3 Then MsgBox quot3次输入错误请退出quot Text1.Locked True End If End Sub 点住文本框去在选择passwordchar填写 4.12 利用输入框输入3个数单击窗体后进行降序排序并求出其中的最大值最小值结果打印在窗体上。
Private Sub Form_Click Dim a As Single Dim b As Single Dim c As Single Dim t As Single a ValInputBoxquotnumberquot b ValInputBoxquotnumberquot c ValInputBoxquotnumberquot If a gt b Then t a a b b t End If If a gt c Then t a a c c t End If If b gt c Then t b b c c t End If Print quot输入的数据为quot amp vbCrLf amp a b c Print quot降序排序后quot amp vbCrLf amp c b a Print quot其中最大值为:quot amp c Print quot其中最小值为quot amp a End Sub 4.15 单击两个命令按钮后分别在文本框中输入N的阶乘和N2的阶乘结果。
Private Sub Command1_Click Dim n As Integer s As Integer i As Integer n ValText1.Text s 1 For i 1 To n s s i Next i Text2.Text s End Sub Private Sub Command2_Click Dim n As Integer s As Integer i As Integer n ValText1.Text s 1 For i 1 To n 2 s s i Next i Text3.Text s End Sub 4.16 使用while…wend循环求2468……2n 大于100000的最小值。
Private Sub Form_Click Dim a As Long i As Long a 1 i 1 While a lt 100000 a a i i i 1 Wend Print a End Sub 4.23 求任意两个1到100之间的正整数的最大公约数和最小公倍数要求两个正整数由随机函数产生。
Private Sub Form_Click m Int100 Rnd 1 n Int100 Rnd 1 a m b n r m Mod n While r ltgt 0 m n n r r m Mod n Wend Print a b Print quotmaxquot n Print quotminquot a b / n End Sub 4.24 单击窗体后产生九九乘法表。
Private Sub Picture1_Click Dim a0 To 10 0 To 10 As Integer Picture1.Print Tab36 quot九九乘法表quot Picture1.Print quot-------------------------------------------------------------------quot For i 1 To 9 For j 1 To 9 ai j i j Next j Next i For i 1 To 9 For j 1 To 9 Picture1.Print i amp quotxquot amp j amp quotquot ai j Next j Picture1.Print Next i Picture1.Print quot-----------------------------------------------------------------quot End Sub 4.25 在窗体上添加四个命令按钮单击各命令在图片框上显示如图所显示不通的字母图形。
Dim i As Integer j As Integer Private Sub Command1_Click For i 1 To 6 For j i To 1 Step -1 Picture1.Print Chr64 j Next j Picture1.Print Next i End Sub Private Sub Command2_Click Dim i As Integer j As Integer For i 1 To 6 Picture1.Print Tab3 i For j 1 To 7 - i Picture1.Print Chr64 j Next j Picture1.Print Next i End Sub Private Sub Command3_Click Dim i As Integer j As Integer For i 1 To 6 Picture1.Print Tab8 - i For j i To 1 Step -1 Picture1.Print Chr71 - j Next j Picture1.Print Next i End Sub Private Sub Command4_Click Dim i As Integer j As Integer For i 1 To 6 For j i To 6 Picture1.Print Chr71 - j Next j Picture1.Print Next i End Sub 4.27 设计一个素数判定器单击生成数按钮则生成一个0倒100之间的整数并显示在text1中单击判定按钮则对产生的数进行判定若是则在text2中显示是素数否则显示不是素数。
Private Sub Command1_Click a Int99 Rnd 0 Text1.Text Vala End Sub Private Sub Command2_Click b ValText1.Text For i 2 To b - 1 If b Mod i 0 Then Exit For End If Next i If i gt b Then Text2.Text quot是素数quot Else Text2.Text quot不是素数quot End If End Sub 4.13 用辗转相除法求两个数mn的最大公约数和最小公倍数。
Private Sub Command1_Click Dim m As Integer n As Integer r As Integer Dim a As Integer b As Integer m ValText1: n ValText2 a m: b n r m Mod n While r ltgt 0 m n n r r m Mod n Wend Text3.Text Strn Text4.Text Stra b / n End Sub 4.15 利用公式π/41-1/31/5-1/5??求π的值精确到小数点后6位。
Private Sub Command1_Click Dim sum As Single pi As Single k As Single Dim t As Integer i As Long t 1 i 1 sum 0 Do k t / i sum sum k t -t i i 2 Loop While Absk gt 0.000001 pi 4 sum Text1.Text Strpi End Sub 5.6 设计一个大小能自由调整以适应图片尺寸的图片框并添加显示和清空两个命令按钮。
Private Sub Command1_Click Picture1.Picture LoadPicturequotH:我的图片P1030969.jpegquot End Sub Private Sub Command2_Click Picture1.Picture LoadPicturequotquot End Sub 5.8 按开始按钮使标签在窗体上从左向右移动当遇到窗体右边缘时改成从右向左运动如此往复。
按停止按钮则停止运动程序运行后文本框每一秒的背景色由红到绿然后绿到蓝再绿到红如此循环。
Private Sub Command1_Click Timer1.Enabled True End Sub Private Sub Command2_Click Timer1.Enabled False End Sub Private Sub Form_Load Timer1.Enabled False End Sub Private Sub Timer1_Timer Static a As Integer b As Integer If a 0 Then Label1.Left Label1.Left 200 If Label1.Left gt Form1.Width - Label1.Width Then a 1 Else Label1.Left Label1.Left - 200 If Label1.Left lt 0 Then a 0 End If Select Case b Case 0 Text1.BackColor vbRed Case 1 Text1.BackColor vbGreen Case 2 Text1.BackColor vbBlue End Select b b 1 If b gt 2 Then b 0 End Sub 5.11 窗体上有一个名为L1的列表框通过属性向列表框中添加3个项目分别为123456789.当启动之后每双击
列表框中任意一项则总在最后添加一项该项包括选中项在内的前面所有项之和。
Private Sub L1_Click For i 0 To L1.ListIndex s s ValL1.Listi Next i L1.AddItem s End Sub Private Sub L1_Click For i 0 To L1.ListCount - 1 s s ValL1.Listi Next i L1.AddItem s End Sub 5.7 控制label1控件中字体的大小和字形格式。
Private Sub Check1_Click Label1.FontItalic Check1.Value End Sub Private Sub Check2_Click Label1.FontUnderline Check2.Value End Sub Private Sub Option1_Click Label1.FontSize 10 End Sub Private Sub Option2_Click Label1.FontSize 13 End Sub Private Sub Option3_Click Label1.FontSize 15 End Sub 5.8 在窗体添加一个图片框在图片框中放4个单选按钮设置各单选按钮style属性为1-graphical用shape控件画出一个红色的长方形设置fillstyle0fillcolor为红色。
Private Sub Command1_Click Shape1.Shape 3 End Sub Private Sub Command2_Click Shape1.Shape 1 End Sub Private Sub Command3_Click Shape1.Shape 0 End Sub Private Sub Command4_Click Shape1.Shape 2 End Sub 5.9 Private Sub Command1_Click For i 0 To List1.ListCount - 1 If List1.Listi Text1.Text Then Exit Sub Next i List1.AddItem Text1.Text End Sub Private Sub Command2_Click List1.RemoveItem List1.ListIndex End Sub Private Sub Command3_Click Text1.Text quotquot List1.Clear End Sub Private Sub List1_Click Text1.Text List1.Text 5.10 窗体上有一个文本框和3个组合框combo1为简单组合框combo2为下拉列表框ccombo3为下拉列表框。
Private Sub Combo1_Click n Combo1.Text If n quot白quot Then Text1.ForeColor vbWhite ElseIf n quot黑quot Then Text1.ForeColor vbBlack ElseIf n quot红quot Then Text1.ForeColor vbRed ElseIf n quot绿quot Then Text1.ForeColor vbGreen ElseIf n quot蓝quot Then Text1.ForeColor vbBlue ElseIf n quot黄quot Then Text1.ForeColor vbYellow End If End Sub Private Sub Combo2_Click n Combo2.Text If n quot白quot Then Text1.BackColor vbWhite ElseIf n quot黑quot Then Text1.BackColor vbBlack ElseIf n quot红quot Then Text1.BackColor vbRed ElseIf n quot绿quot Then Text1.BackColor vbGreen ElseIf n quot蓝quot Then Text1.BackColor vbBlue ElseIf n quot黄quot Then Text1.BackColor vbYellow End If End Sub Private Sub Combo3_Click n Combo3.Text If n quot左quot Then Text1.Alignment 0 ElseIf n quot中quot Then Text1.Alignment 2 ElseIf n quot右quot Then Text1.Alignment 1 End If End Sub 5.11 华氏和摄氏的关系为摄氏5华氏-32/9. Private Sub VScroll1_Change Label5.Caption VScroll1.Value Label6.Caption Format5 VScroll1.Value - 32 / 9 0 End Sub 5.14 设计一个程序使得文本框背景颜色每隔一秒由红色绿色。
Private Sub Timer1_Timer Static i As Integer Select Case i Mod 2 Case 0 Label1.BackColor vbRed Case 1 Label1.BackColor vbGreen End Select i i 1 End Sub 5.15 让一行欢迎词从左到右来回移动直到按下停止。
Private Sub Command1_Click Timer1.Enabled False End Sub Private Sub Timer1_Timer Static i As Integer Select Case i Mod 2 Case 0 Label1.Left Label1.Left 100 If Label1.Left gt Form1.Width - Label1.Width Then i 1 Case 1 Label1.Left Label1.Left - 100 If Label1.Left lt.