vb实验编程答案

合集下载

vb程序设计实验教程习题答案(精品).doc

vb程序设计实验教程习题答案(精品).doc

实验教材实验-1:单项选择题:AACCC DCBBC2:填空题:(1).bas(2).vbp, ,frm(3)视图工具箱(4)对象(5)属性对象事件方法(6)Windows(7)事件行为(8)窗体模块实验二1:单项选择题:ADCCA BAB DC BBCBC BAB DC 2:基本运算题:(1)27(2) 3.33333333333333(3) 3(4)0(5)-3(6) 2(7)ab33(8)类型不匹配(9) 3(10)AB3实验三1:单项选择题:BCBDC2:填空题:(1)300(2)321456(3)Shanghai(4) 6 7 6(5)True(6)2+3=5(7)”area=” 12.56(8)VISUAL实验四1:单项选择题:CAACA2:填空题:(1)7(2) 3 5(3)输入的数小于0(4) 3(5)False(6)2010 is not leap year.试验五1:单项选择题:ACCAB ACCCB2:填空题:(1)9(2)@n ② 1(3)®Len(a)②n\2 ③Mid(a,(n-i+l),l)④Mid(a,(n-i+l),l)(4)@n Mod 3=1 And n Mod 5=1 And n Mod 7=1 ©While CountN<5(5)Mid(cl,l,i)实验六1:单项选择题:DB DCD ACADD2:填空题:(1)30(2) 3(3)①a(2)②a(3)③a(l)(4)①1 ②2 ③a(i)=a(6-i)(5)①i+j ②0 ③a(i,j)(6)©Print ②k ③a(k,j)=a(j,k)④a(k,j);实验七1:单项选择题:ABBACB2:填空题:(1)15(2)①Preserve a(n+l) ②a(i+l)=a(i) ③Exit For ④a(i+l)=m⑤0 to n+1 (或LBound(a) to UBound(a))(3)d,c,b,a实验八1:单项选择题:CADBD CACB2:填空题:(1)a=9 b=4(2)362516941(2)①a (2)UBound(b)③实验九1:单项选择题:DDDBB CAB CAD2:填空题:(1)①p=p*n ②Fac(n) ③ 10人-6(2)①Max=x ②Max=y ③p ④Max(x,a(n))(3)①number(i) ②n as integer (3)number=number+7* 10A(i-1)实验十1:单项选择题:DC BAB C实验十一1:单项选择题:DADBBB BDD实验十二1:单项选择题:CBCBC CBDAD DBC2:填空题:(1)①Label 1. FondName=Combo 1 .TextCombo2.Text ③ Label 1 .FontBold=True ④ Label 1 .FondItalic=True⑤ Label 1 .FondSize=Combo3.Text(2)①Listl.Text ©List 1 .RemoveitemListl.ListindexListl .Additem List2.Text ④ List2.RemoveItem实验十三实验十四1:单项选择题:DACDAABC2:填空题:(1)ShowFont(2)打开另存为颜色字体打印机帮助(3)工程部件Microsoft Common Dialog Control 6.0(4 ) ShowColor Action=3实验十五1:单项选择题:CB BCB B BAD2:填空题:(1)内建菜单弹出菜单(2)Click(3)工具(4)代码窗口Click(5)相同不同实验十六1:单项选择题:B CAAC ACDDB DCC2:填空题:(1)①Open "c:\studl.txt” For Output As #1Textl.Text=”End”White #l,Textl.Text(2)①For Input ②For Output As #2 ③Not Eof(l)Input #l,strl ⑤ Close #1,#2 ⑥Kill(“c:\old.txt”(3)①For Output ②For Input As #2。

VB编程题答案

VB编程题答案

Vb编程题1.求自然数1~100之间奇数之和。

Private Sub Form_Click()Dim s As Long, i As Integers = 0For i = 1 To 100 Step 2s = s + iNextPrint sEnd Sub2.求自然数1~100之间偶数之和。

Private Sub Form_Click()Dim s As Long, i As Integers = 0For i = 2 To 100 Step 2s = s + iNextPrint sEnd Sub3.编写程序,在窗体上随机输出一个10--99之间的两位整数。

Private Sub Form_Click()clRandomizePrint Int(Rnd() * 90) + 10End Sub4.编程计算从200到1000之间的所有4的倍数之和。

Private Sub Form_Click()clsSum = 0For i = 200 To 1000 Step 4Sum = Sum + iNextPrint "200-1000中4的倍数和是:"; SumEnd Sub5. 编程计算从300到1000之间的所有7的倍数之和。

Private Sub Form_Click()Dim i As Integer, sum As Longsum = 0For i = 300 To 1000If i Mod 7 = 0 Thensum = sum + iEnd IfNextPrint "300-1000中7的倍数和是:"; sumEnd Sub6.编写程序在窗体上计算:5/6+4/5+3/4+2/3+1/2Private Sub Form_Click()Dim s As Single, i As Integer, t As Singles = 0For i = 5 To 1 Step -1t = i / (i + 1)s = s + tNextPrint sEnd Sub7.在窗体的单击事件中编写程序,从键盘上任意输入3个数,输出最大数。

vb实验报告(答案)

vb实验报告(答案)

vb实验报告(答案)Vb程序设计实验项目试验报告(1)院(系): 课程名称: 日期: 班级学号实验室专业姓名计算机号实验成绩评定名称 VB环境的熟悉与应用所用教师签名软件 Visual Basic 6.0实验目 1(熟悉VB的系统环境,了解VB程序的编写、保存和编译的方法过程。

的 2(初步了解几个常用控件的使用方法。

或要求1、在vb中____C_____被称为对象。

实A.窗体B.控件C.窗体和控件D.窗体、控件和属性验2、设在名称为form1的窗体上只有1个名称为C1的命令按钮,下面叙述中正确的是( B ) 步A.窗体的Click事件过程的过程名是form1_Click 骤B.命令按钮的Click事件过程名是C1_Click、 C.命令按钮的Click事件过程的过程名是Command1_Click 心3、编译执行程序的快捷键是_Ctrl+F5_。

得 4、启动visual basic 后标题栏中显示的信息是(C)。

A.工程-Mmicrosoft Visual Basic[设计] 体B.工程-Mmicrosoft Visual Basic[中断]会C.工程1-Mmicrosoft Visual Basic[设计]第 1 页共 53 页1/53页Vb程序设计实验项目D.工程1-Mmicrosoft Visual Basic[中断]5、Visual Basic集成开发环境主要窗口包括__窗体设计窗口;代码设计窗口;_属性窗口;_工程资源管理器窗口_;_工具箱窗口。

6、属性显示方式分两种,即按__字母顺序和按_分类__顺序,分别通过单击相应的按钮来实现。

7、编一程序,程序运行的初始界面如图1-1所示,当用户在文本框中输入姓名,例如输入“张三”,单击“你输入的姓名是:”按钮(command1),则程序的运行情况如图1-2所示,如果单击“结束”按钮(command2),即结束程序运行。

注意:保存时必须存放在自己创建的文件夹下,窗体文件名为kt1.frm,工程文件名为kt1.vbp。

VB-编程答案

VB-编程答案

选择控制结构1. 编写程序,计算如下分段函数并输出结果,其中x 的值由键盘输入。

Private Sub Command1_Click()Dim x, y As Integerx = InputBox()If x < 0 Theny = x * x + xElseIf x = 0 Theny = 100Elsey = x * x - xEnd IfPrint "y="; yEnd Sub2. 编写程序,任意输入3个整数,按从大到小的顺序依次输出。

Dim x%, y%, z%, t%x = Text1.Texty = Text2.Textz = Text3.TextIf x < y Thent = xx = yy = tEnd IfIf y < z Thent = yy = zz = tIf x < y Thent = xx = yy = tEnd IfEnd IfPrint x; y; z3. 编写程序,任意输入3个整数,按从小到大的顺序依次输出。

Dim x%, y%, z%, t%x = Text1.Texty = Text2.Text⎪⎩⎪⎨⎧>-⨯=<+⨯=)()()(0x 0x 1000x x x x x x x yz = Text3.TextIf x > y Thent = xx = yy = tEnd IfIf y > z Thent = yy = zz = tIf x > y Thent = xx = yy = tEnd IfEnd IfPrint x; y; z4.编写程序,任意输入一个整数,判断它是奇数还是偶数,要求:用文本框输入整数,用标签显示结果,画出设计窗体效果。

Private Sub Command1_Click()Dim x%x = Text1.TextIf x Mod 2 = 0 ThenLabel1.Caption = x & "是偶数"ElseLabel1.Caption = x & "是奇数"End IfEnd Sub5.编写程序,任意输入一个整数,在标签上显示该整数的绝对值,要求:用文本框输入整数,用标签显示结果,画出设计窗体效果。

vb实验编程答案

vb实验编程答案

实验教程实验三顺序结构程序设计编程题参考答案(第47页)1. 输入学生的语文、数学、英语、物理四门课程的成绩,计算该学生的总成绩和平均成绩并输出。

要求使用InputBox分别进行输入,使用Print 方法输出。

2. 输入圆的半径,输出其周长、面积。

要求使用TextBox控件进行输入,使用Label 控件输出。

3. 输入一元二次方程ax2+bx+c=0 的系数a、b、c,输出两个实根。

要求使用TextBox 控件分别进行输入,使用Label控件输出。

实验四选择结构程序设计编程题参考答案(第58页)1. 输入一个,判断其是否能同时被3和7整除。

2. 输入三个数,输出其中的最大值。

6. 编程及输出分段函数的值。

由键盘输入t 的值,计算y 的值7. 输入年龄,输出所处人群:9岁以下为儿童,输出A ;10~19为少年,输出B ;20~29为青年,输出B ;30~49为中年,输出B ; 50以上为老年,输出E ;要求使用Select Case 语句实现。

8. 输入年份,输出对应的属相。

已知1984年为鼠年,要求使用Select Case 语句实现。

实验五 循环结构程序设计编程题参考答案 (第77页)1. 编写程序,求s=1×2×3 + 2×3×4 + … + n ×(n+1) ×(n+2)的值,n 由程序输入。

2.有一个分数序列 ,求出这个序列前20项的和。

Y=t 2 t<1 t 2-1 1≤t<2 t 2-2t+1 2≤t<3 t 2+4t-17 t ≥3, (13)21,813,58,35,23,123. 求∑=101!nn(即1!+2!+3!+…+10!)4. 求一类三位数,该书中有某两位为相同数字,且该数是一个完全平方数,输出这些数并统计个数。

5. 输出所有的“水仙花数”。

“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

VB实验报告参考答案(本部)

VB实验报告参考答案(本部)

实验报告参考答案(本部)实验一VB集成开发环境实验内容(3):四、设计流程①界面设计:在窗体上画一个标签。

②属性设置:标签的width设为2000,height设为300。

有边框是BordStyle设为1,背景透明是BackStyle设为0。

显示“等级考试”是将caption设为“等级考试”,居中是将Alignment 设为2。

③代码设计:无五、关键代码(注意:解题方法有多种,其它方法只要能完成实验任务也是正确的)无六、调试和测试结果至少要写测试结果正确,如果写了调试中出现的典型问题及解决方法,分数给高些实验二简单的VB程序设计实验内容(3):四、设计流程①界面设计:在窗体上画一个标签、一个命令按钮。

②属性设置:固定边框是BordStyle设为1,背景透明是BackStyle设为0。

③代码设计:分别在3个事件过程form_load(),form_click(),Cmd1_click()中编程。

其中图片从网上下载,与工程文件、窗体文件保存在同一目录,代码中使用相对路径app.path。

五、关键代码(注意:解题方法有多种,其它方法只要能完成实验任务也是正确的)Private Sub Form_Load()Form1.Picture = LoadPicture(App.Path + "\tu.jpg")Form1.Caption = "我的变化"End SubPrivate Sub Form_Click()LBL1.Top = 0LBL1.Left = 0End SubPrivate Sub Cmd1_Click()Cmd1.Height = 1500Cmd1.Width = 2000End Sub六、调试和测试结果至少要写测试结果正确,如果写了调试中出现的典型问题及解决方法,分数给高些实验三数据类型及其运算四、设计流程①界面设计:在窗体上画1个文本框、1个标签、6个命令按钮。

VB编程实验报告答案 完整版

VB编程实验报告答案 完整版

实验一:一、选择1.D2.D3.B4.A5.B6.C7.CD8.D9.D 10. BC11. A 12.D 13. D 14. C 15. A二、填空1.windows2. 中断3.工具箱4.代码5.分类6.form2.show7. 出现语法错误8. 错9. 事件10. 面向对象11.工程文件结构12. 工程13. 设计程序代码14. 双击窗体15. C:\Program Files\Microsoft Visual Studio\VB98 或者VB的安装文件夹16.属性17.方法18.font 19. form_load() 20.对象实验二:一、选择1.B2.C3. B4.C5.C6.C7.B8.B9.D 10. A11. C 12.C 13. C 14. B 15. A16.C 17.B 18.D 19.C 20. C二、填空1. Form_Click()2. Interval、50003..vbp .frm4.left top Height、Width twip5.picture6.load7. locked8. font Alignment9. GotFocus() LostFocus() 10. 属性窗口程序代码实验三:一、选择1.A2.BC3.BC4.A5.A6.A7.BCD8.D9.B 10. B11. B二、判断题不合法的变量:3. 4. 5. 6. 8. 10. 11. 14.15. 16.三、1、abs(x+y)2. (3+x*y)^23. (-b+sqr(b^2-4*a*c))/(2*a)4.sin(30*3.14/180)+exp(2)四、1. int(rnd* 71+20) 或int(rnd* 71)+202. x<0 and y>0 or x>0 and y<0 或者x*y<03. x mod 3=0 or x mod 7=0或x/3=x\3 or x/7=x\74. x>=10 and x<205. x>="A" and x<="Z"6. left(s,3)实验四:一、选择1.C2.D3. C4.D5.A6.D7.B8.B9.A 10. C二、填空1. 48.71E-01, -4.87122. 关系、逻辑、算术3. 900,100,mid(s,2,1)4.m、1,2,3 、print m & "月是第二季度"、end select 三、1. X=4 y=132. 10实验五一、选择B C B A C二、填空(1)for x=2 to 10s*(1-1/x)(2) 1 t>1E-6(3) j t(4) 5 11 5实验六一、选择B C C C D D D D C A实验七一、选择1C 2C 3D 4A 5B 6D 7AB 8A 9A 10A 二、填空(1)n!(2)1+1+2+3+4+……+n(3)n是否是素数(4)冒泡排序(5)递归调用产生斐波那契数列(6)求最大公约数和最小公倍数第八页编程2-1Private Sub Command1_Click()Label3 = Val(Text1) * Val(Text2)End SubPrivate Sub Command2_Click()Label3 = Val(Text1) / Val(Text2)End Sub2-2Private Sub Command1_Click()Picture1.Picture = LoadPicture("d:\tu1.jpg") End SubPrivate Sub Command2_Click()Picture1.Picture = LoadPicture("d:\tu2.jpg") End SubPrivate Sub Command3_Click()Picture1.Visible = FalseEnd Sub2-5Private Sub Form_Load()Image1.Picture = LoadPicture("C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Sunset.jpg")End SubPrivate Sub Command1_Click()Timer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()Timer1.Enabled = FalseEnd SubPrivate Sub Image1_Click()Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer()Image1.Left = Int(Rnd * (Form1.ScaleWidth - Image1.Width)) Image1.Top = Int(Rnd * (Form1.ScaleHeight - Image1.Height)) End Sub第11页编程2-3Private Sub Command1_Click()Dim x%, y%x = Int(Rnd * 900 + 100)Print "产生的随机数:"; xy = (x Mod 10) * 100 + (x \ 10 Mod 10) * 10 + x \ 100Print "反序数:"; Format(y, "000")End SubPrivate Sub Command2_Click()Dim x%, y As Stringx = Int(Rnd * 900 + 100)Print "产生的随机数:"; xy = Trim(Str(x))Print "反序数:" & Right(y, 1) & Mid(y, 2, 1) & Left(y, 1)End SubPrivate Sub Form_Load()End Sub2-4Private Sub Command1_Click()Dim m%, n%Randomizem = Val(Text1): n = Val(Text2)x = Int(Rnd * (n - m + 1) + m)y = Int(Rnd * (n - m + 1) + m)z = Int(Rnd * (n - m + 1) + m)Label3 = m & "到" & n & "之间的3个随机数为: " & x & Space(3) & y & Space(3) & zEnd SubPrivate Sub Form_Load()End Sub2-5Private Sub Command1_Click()Label1.Caption = DateEnd SubPrivate Sub Command2_Click()Label2.Caption = TimeEnd SubPrivate Sub Command3_Click()Label3.Caption = WeekdayName(Weekday(Date)) End SubPrivate Sub Form_Load()End Sub第十五页2-1方法一Private Sub Command1_Click()Dim x!, a!, aa%, b!, bb%, c!, cc$x = Val(Text1)a = x * 180 / 3.14aa = Int(a)b = (a - aa) * 60bb = Int(b)c = (b - bb) * 60cc = Format(c, "#.000")Label2 = "弧度" & Text1 & "转换为" & aa & "度" & bb & "分" & cc & "秒"End Sub方法二Private Sub Command1_Click()x = InputBox("请输入弧度数值", "弧度转换", 0)xx = Val(x)a = x * 180 / 3.14aa = Int(a)b = (a - aa) * 60bb = Int(b)c = (b - bb) * 60cc = Format(c, "#.000")MsgBox "弧度" & x & "转换为" & aa & "度" & bb & "分" & cc & "秒"End Sub2-2Private Sub Command1_Click()Dim x%x = Val(Text1)If x / 400 = x \ 400 Or x Mod 4 = 0 And x Mod 100 <> 0 ThenLabel2 = Text1 & "年是闰年"ElseLabel2 = Text1 & "年不是闰年"End IfEnd Sub2-3方法二Private Sub Command1_Click()Dim x%, y%, z%x = InputBox("输入第一个数")y = InputBox("输入第二个数") z = InputBox("输入第三个数") If x > y Then t = x: x = y: y = t If y > z Then t = y: y = z: z = t If x > y Then t = x: x = y: y = t Print x, y, zEnd SubPrivate Sub Form_Load()End Sub方法一Private Sub Command1_Click() Dim x%, y%, z%x = Val(Text1)y = Val(Text2)z = Val(Text3)If x > y Then t = x: x = y: y = t If y > z Thent = y: y = z: z = tIf x > y Thent = x: x = y: y = tEnd IfLabel1 = x & Space(5) & y & Space(5) & z End Sub2-4方法一Private Sub Command1_Click()x = Val(InputBox("请输入上网时间:"))If x <> -1 ThenSelect Case xCase Is < 10fei = 25Case 10 To 50fei = x * 2Case 50 To 100fei = x * 1.5Case Is >= 100fei = x * 1End SelectMsgBox "上网费用为:" & fei & "元"End IfPrivate Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()End Sub方法二Private Sub Command1_Click()Dox = Val(InputBox("请输入上网时间:", , -1)) If x <> -1 ThenSelect Case xCase Is < 10fei = 25Case 10 To 50fei = x * 2Case 50 To 100fei = x * 1.5Case Is >= 100fei = x * 1End Selectzongfei = zongfei & Space(3) & fei MsgBox zongfei, , "上网费用为" ElseExit DoEnd IfLoopEnd SubPrivate Sub Command2_Click() EndEnd SubPrivate Sub Form_Load()End Sub2-5第18页2-3Private Sub Command1_Click()n = 0DoIf n Mod 2 = 1 And n Mod 3 = 2 And n Mod 5 = 4 And n Mod 6 = 5 And n Mod 7 = 0 Then Exit Don = n + 1LoopPrint nEnd Sub]2-5Private Sub Command1_Click()x = Val(Text1)y = x * 4n = 0Do While x <= yx = x * (1 + 0.1)n = n + 1Print xLoopPrint x, nEnd Sub2-6Private Sub Command1_Click()n = Val(InputBox("输入计算到第几项:", , 0)) For i = 1 To ns = s + (-1) ^ (i + 1) * iNextPrint n, sEnd SubPrivate Sub Form_Load()-8End Sub2-8Private Sub Command1_Click()Dim s As Double, pi As Doublen = 1: s = 0Do While 1 / (2 * n - 1) >= 0.0001s = s + (-1) ^ (n + 1) * 1 / (2 * n - 1)n = n + 1Looppi = 4 * sPrint piEnd Sub2-9Private Sub Command1_Click()For n = 100 To 999k = Trim(Str(n))If n = Left(k, 1) ^ 3 + Mid(k, 2, 1) ^ 3 + Right(k, 1) ^ 3 Then Print nNextEnd Sub2-11Private Sub Command1_Click()s = 0: Max = 0: Min = 1000For i = 1 To 20Randomizex = Int(Rnd * 50 + 51)Print x;If x > Max Then Max = xIf x < Min Then Min = xs = s + xNext iaver = s / 20PrintPrint "max="; Max, "min="; Min, "aver="; aver End SubPrivate Sub Form_Load()End Sub。

vb实验指导后题目-参考答案

vb实验指导后题目-参考答案

期末自测题(A卷)文件部分的选择题要做,其它类型的题目不用做,不作考察选择题其它部分还没有时间做一、单选题(每题1分,共25分)1、211.66.160.1 是Internet上的( )。

(A) IP地址 (B)域名 (C)密码 (D)软件编号2、在以下四个www网址中,()网址不符合www网址书写规则。

(A) (B) (C) (D) 3、下列字符中,其ASCII码值最小的一个是()。

(A) A (B) a (C) Z (D) X4、下列软件中属于浏览器的是()。

(A) Excel (B) Internet Explorer (C) QQ (D) Visual Basic5、电子邮件(E-mail)地址的正确形式是()。

(A)用户名@域名 (B)用户名#域名 (C)用户名/域名 (D)用户名.域名6、下面变量名不合法的是()。

(A) a2 (B) abcd (C) a$x (D) Cd_E7、下列表达式中,正确的Visual Basic表达式是()。

(A) 2sin45° (B) ∏*R2 (C) x÷y×5 (D) (x+1)/(x^2+1)/28、下面逻辑表达式的值为真的是()。

(A) "A"> "a" (B) "abc"> "bc"(C) "That"> "Thank" (D) 12> 12.19、下列过程定义语句说明不合法的是()。

(A) Sub f1(ByRef n() As Integer) (B) Sub f1(ByRef n As Integer)(C) Sub f1(ByVal n() As Integer) (D) Sub f1(ByVal n As Integer)10、执行下面的4个打印语句,将出错的是()。

VB编程题目及答案

VB编程题目及答案

1.输入3个数,输出最大的数2.有一元二次方程:aX2+bX+c=0,依次输入a,b,c,判断此方程是否有实根3.输入一个百分制的成绩,若大于等于60,则用消息框弹出,显示“恭喜!成绩通过!”,若小于60,则弹出消息框,显示“抱歉!成绩没有通过!”4.输入一年份,判断它是否为闰年,并显示有关信息。

(判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除)5.输入学生的百分制成绩,转换输出为五级制成绩,90~100分转换为优秀,80~89转换为良好,70~79转换为中等,60~69转换为及格,0~60转换为不及格。

6.随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。

例如,产生246,输出是642。

7.输入两个整数,求它们的最大公约数。

8.编程求200--400范围内5的倍数或7的倍数之和。

(一个数如果同时是7和5的倍数,则只能加一次。

9.计算100~300之间所有能被3和7整除的数之和10.分别统计1-100中,满足3的倍数、7的倍数的数各有多少。

11.某次歌曲大奖赛,有7个评委。

如果分别输入7个评委对某个参赛者的打分数,按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。

12.输入一串字符,统计其中字母A(不区分大小写)出现的次数。

13.我国目前有13亿人口,按人口年增长率0.8%计算,多少年后我国人口将超过26亿。

14.小猴有桃若干,第一天吃掉一半多一个;第二天吃剩下桃子的一半多一个;以后每天都吃尚存桃子的一半多一个,到第7天只剩一个,问小猴原有桃多少?15.求100以内的素数16.百元买百鸡问题。

假定小鸡每只5角,公鸡每只2元,母鸡每只3元,编程求解购鸡方案。

17.编程求斐波那契数列前11项。

(斐波那契数列:F(0)=f(1)=1, F(n)=F(n-1)+F(n-2) n>=2 )18.将输入的字符串以反序显示。

例如:输入“ASDFGT”,显示“TGFDSA”19.随机产生10个[30,100]内的整数,求最大值及所对应的下标20.随机产生10个[30,100]内的整数,求最大值、最小值和平均值。

VB实验及答案

VB实验及答案

对象名称
属性名称
Form1
Caption
Command1 Caption
Label1
Caption
Label2
Caption
Label3
Caption
Text1、Text2 Text
属性值 象限判断 判断 X= Y=
(3)编写事件代码: Private Sub Command1_Click() Dim x!, y!
Private Sub Command2_Click() Dim f!, c! c = Val(Text2.Text) f = 9 / 5 * c + 32 Text1 = Str(f)
End Sub
(4)程序的运行及保存。 4 求圆的面积及圆柱体的体积,界面如图 2-3 所示。
【步骤】 (1)设计应用程序的界面:在窗体上创建两个命令按钮 Command1 和 Command2,四个标签 Label1、Label2、Label3 和 Label4,四个文本框 Text1、Text2、Text3 和 Text4。 (2)设置对象的属性
【步骤】
( 1 ) 在 窗 体 中 创 建 一 个 标 签 Label1 、 一 个 命 令 按 钮 Command1。
(2)在属性窗口中设置对象的属性:
对象名称
属性名称
属性值
Form1
Caption
设计界面
Label1
Caption
请留意此处
Command1
Caption
单击此处
(3)在 Command1 的 Click 事件中编写程序代码:
x = Val(Text1) y = Val(Text2) If x > 0 Then

VB实验参考答案

VB实验参考答案

VB实验参考答案VB实验参考答案VB(Visual Basic)是一种常用的编程语言,广泛应用于软件开发领域。

在学习VB的过程中,进行实验是非常重要的一部分。

本文将为大家提供一些VB实验的参考答案,帮助大家更好地理解和掌握VB编程。

实验一:简单的计算器程序要求:编写一个简单的计算器程序,能够实现基本的加减乘除运算。

参考答案:```vbPrivate Sub btnCalculate_Click()Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text)num2 = CDbl(txtNum2.Text)If rbAdd.Checked Thenresult = num1 + num2ElseIf rbSubtract.Checked Thenresult = num1 - num2ElseIf rbMultiply.Checked Thenresult = num1 * num2ElseIf rbDivide.Checked ThenIf num2 <> 0 Thenresult = num1 / num2ElseMsgBox("Error: Division by zero!")End IfEnd IftxtResult.Text = resultEnd Sub```实验二:学生成绩管理系统要求:编写一个学生成绩管理系统,能够实现学生信息的录入、查询和统计功能。

参考答案:```vbPrivate Sub btnAdd_Click()Dim name As StringDim score As Integername = txtName.Textscore = CInt(txtScore.Text)lstStudents.Items.Add(name & " - " & score)End SubPrivate Sub btnSearch_Click()Dim searchName As StringDim index As IntegersearchName = txtSearch.Textindex = lstStudents.FindString(searchName)If index <> -1 ThenlstStudents.SelectedIndex = indexElseMsgBox("Student not found!")End IfEnd SubPrivate Sub btnStatistics_Click()Dim totalScore As IntegerDim averageScore As DoubleDim count As IntegerFor Each item As String In lstStudents.ItemsDim score As Integer = CInt(item.Split("-")(1).Trim()) totalScore += scorecount += 1NextaverageScore = totalScore / counttxtTotalScore.Text = totalScoretxtAverageScore.Text = averageScore```实验三:简单的图书管理系统要求:编写一个简单的图书管理系统,能够实现图书信息的录入、查询和删除功能。

vb实验作业参考答案

vb实验作业参考答案

vb实验作业参考答案VB实验作业参考答案引言:Visual Basic(VB)是一种基于事件驱动的编程语言,广泛应用于Windows操作系统的软件开发。

在学习VB编程过程中,实验作业是非常重要的一部分,通过实践来巩固所学知识。

本文将提供一些VB实验作业的参考答案,帮助读者更好地理解和掌握VB编程。

一、VB基础知识实验作业参考答案1. 实验题目:编写一个VB程序,实现两个整数的加法运算,并将结果显示在文本框中。

参考答案:```Private Sub btnAdd_Click()Dim num1 As IntegerDim num2 As IntegerDim result As Integernum1 = CInt(txtNum1.Text)num2 = CInt(txtNum2.Text)result = num1 + num2txtResult.Text = CStr(result)End Sub```2. 实验题目:编写一个VB程序,实现输入一个数字,判断该数字是奇数还是偶数,并在消息框中显示结果。

参考答案:```Private Sub btnCheck_Click()Dim num As Integernum = CInt(txtNum.Text)If num Mod 2 = 0 ThenMsgBox "该数字是偶数"ElseMsgBox "该数字是奇数"End IfEnd Sub```二、VB控件应用实验作业参考答案1. 实验题目:编写一个VB程序,实现一个简单的计算器功能,包括加法、减法、乘法和除法操作。

参考答案:```Private Sub btnAdd_Click()Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 + num2txtResult.Text = CStr(result) End SubPrivate Sub btnSubtract_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 - num2txtResult.Text = CStr(result) End SubPrivate Sub btnMultiply_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 * num2txtResult.Text = CStr(result) End SubPrivate Sub btnDivide_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text)num2 = CDbl(txtNum2.Text)If num2 = 0 ThenMsgBox "除数不能为0"Elseresult = num1 / num2txtResult.Text = CStr(result)End IfEnd Sub```2. 实验题目:编写一个VB程序,实现一个简单的登录界面,包括用户名和密码输入框以及登录按钮。

VB实验教材习题答案 (1)

VB实验教材习题答案 (1)

第1章VB集成环境与VB概述一、选择题第2章常用控件与界面设计一、选择题第3章数据、表达式与简单程序设计一、选择题第4章选择分支与循环一、选择题二、填空题1.5 7 13 12 14 112. 3 1 5 3 4 13. right(a$,i)4. 45. 6-i6. 6 11 10 21 13 347. 5 14 138. Is else end select9. a*(2*i-1)*(-1)*x/(2*i+2) i+110. 0 n-111. 1 t*(-1)*x^2/((2*k-1)*(2*k)) Abs(t)>eps12. count1=count+1 count1>013. sum=0 sum+j st & k & “+”14. 9 x三、编程题1.Option ExplicitPrivate Sub Command1_Click()Dim s As String, i As Integer, ch As String * 1Dim a As Integer, b As Integer, c As Integers = Text1.TextFor i = 1 To Len(s)ch = Mid(s, i, 1)If (ch >= "0" And ch <= "9") Thena = a + 1ElseIf (ch >= "A" And ch <= "Z") Or (ch >= "a" And ch <= "z") Thenb = b + 1Elsec = c + 1End IfNextPrint "数字个数"; aPrint "字符个数"; bPrint "其他字符"; cEnd Sub2.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, n As IntegerDim a As Integer, b As Integer, c As IntegerFor i = 1 To 50n = Int(Rnd * 90 + 10)Print n;If i Mod 10 = 0 Then PrintIf n <= 40 Thena = a + 1ElseIf n <= 70 Thenb = b + 1Elsec = c + 1End IfNext iPrint "小于等于40的个数:"; aPrint "大于40小于等于70的个数:"; bPrint "大于70的个数:"; cEnd Sub3.Option ExplicitPrivate Sub Command1_Click()Dim x As SingleDim t As Long, n As Integerx = 1t = 1n = 1Do While 1 / t >= 0.0001x = x + 1 / tn = n + 1t = t * nLoopPrint xEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim k As Integer, sum As Integer, i As Integerk = InputBox("请输入一个正整数k", "输入框")For i = 1 To Len(CStr(k))sum = sum + Mid(k, i, 1)Next iMsgBox k & "的各位数字之和" & sumEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, iSum As LongFor i = 1 To 20iSum = iSum + i ^ 4NextPrint iSumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a As Integer, b As IntegerDim sum1 As Integer, sum2 As LongDim i As Integer, j As IntegerFor a = 2 To 3000sum1 = 0sum2 = 0For i = 1 To a \ 2If a Mod i = 0 Then sum1 = sum1 + i Next ib = sum1For j = 1 To b \ 2If b Mod j = 0 Then sum2 = sum2 + j Next jIf sum2 = a And a < b Then Print a, bNextEnd Sub7.Private Sub Command1_Click() '打印菱形Dim i As Integer, j As IntegerClsFor i = 1 To 6 '打印菱形上边6行Print Tab(20);Print Spc(6 - i);For j = 1 To 2 * (i - 1) + 1If j = 1 Or j = 2 * (i - 1) + 1 ThenPrint "*";ElsePrint " ";End IfNext jPrintNext iFor i = 5 To 1 Step -1 '打印下半部分Print Tab(20);Print Spc(6 - i);For j = 1 To 2 * (i - 1) + 1If j = 1 Or j = 2 * (i - 1) + 1 ThenPrint "*";ElsePrint " ";End IfNext jPrintNext iEnd Sub8.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, j As Integer, iSum As IntegerPrint "连续和为1250的正整数是:"For i = 1 To 500iSum = 0For j = i To 500iSum = iSum + jIf iSum >= 1250 Then Exit ForNextIf iSum = 1250 ThenPrint i; " ~"; jEnd IfNextEnd Sub9.Option ExplicitPrivate Sub Command1_Click()Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 50For b = a To 50For c = b To 50If a ^ 2 + b ^ 2 = c ^ 2 ThenPrint a, b, cEnd IfNext cNext bNext aEnd Sub第5章数组一、选择题二、填空题1.Variant2.ReDim A(N)A(1)If M < A(I) Then M = A(I)3.Int(26 * Rnd) + 65Chr(t)i + 1 To 10A(i) > A(j)4.InStr(s, ",")Right(s, Len(s) - n)s5.T – 1I + 16.k = 6 - i – jmax = m7.j = 48j = j-38.i + 1i = i + 19. 23710. 3103511.numi + 1a(j)=temp三、编程题1.Option ExplicitOption Base 1Private Sub Command1_Click()Dim a(10) As IntegerDim i As Integer, max As IntegerFor i = 1 To 10a(i) = Int(90 * Rnd) + 10Print a(i);Next iPrinti = 1max = 0Do While i <= 10If a(i) Mod 3 = 0 ThenIf a(i) > max Thenmax = a(i)End IfEnd Ifi = i + 1LoopIf max = 0 ThenPrint "无要找的数"ElsePrint max & "是最大的能被3整除的数"End IfEnd Sub2.Option ExplicitOption Base 1Private Sub Command1_Click()Dim A() As IntegerDim I As Integer, m As IntegerDim K As IntegerReDim A(10)Print "原数组:";For I = 1 To 10A(I) = IPrint A(I);Next IPrintm = InputBox("????m")K = 9 + mReDim Preserve A(K)For I = 11 To KA(I) = A(I - 10)Next IFor I = 1 To 10A(I) = A(I + m - 1)Next IPrint "平移后的数组:";For I = 1 To 10Print A(I);Next IPrintEnd Sub3.Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, J As Integer, K As IntegerDim a() As Integer, n As Integer, p As Integer Dim s As String, Flag As Booleanp = 1For K = 10 To 100I = 1Do While I <= 10 And Not FlagJ = 1Do While J <= 10 And Not Flagn = I ^ 2 + J ^ 2If n = K ThenFlag = TrueReDim Preserve a(p)a(p) = Kp = p + 1s = I & Str(J) & Str(K)List1.AddItem sEnd IfJ = J + 1LoopI = I + 1LoopFlag = FalseNext KEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim A(10) As IntegerDim B(10) As Integer, S As StringDim I As Integer, J As Integer, X As IntegerA(1) = Int(90 * Rnd) + 10S = A(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = A(J) Then Exit ForNext JIf J = I ThenA(I) = XS = S & " " & A(I)Exit DoEnd IfLoopNext IText1.Text = SB(1) = Int(90 * Rnd) + 10S = B(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = B(J) Then Exit ForNext JIf J = I ThenB(I) = XS = S & " " & B(I)Exit DoEnd IfLoopNext IText2.Text = SS = ""For I = 1 To 10For J = 1 To 10If A(I) = B(J) ThenS = S & " " & A(I)End IfNext JNext IIf S = "" ThenText3.Text = "没有重复"ElseText3.Text = SEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As IntegerDim a() As Integer, K As Integer, Sum As Integer K = Val(Text1.Text)I = 1DoJ = K Mod 10ReDim Preserve a(I)a(I) = JSum = Sum + JK = K \ 10I = I + 1Loop Until K = 0Text2.Text = SumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a(100, 2) As Integer, K As IntegerDim I As Integer, J As IntegerDim M As Integer, N As IntegerK = 1For I = 1 To 99For J = 1 To 99If I > J ThenM = I - JN = I + JIf Sqr(M) = Int(Sqr(M)) And Sqr(N) = Int(Sqr(N)) ThenList1.AddItem I & " " & Ja(K, 1) = Ia(K, 2) = JK = K + 1End IfEnd IfNext JNext IEnd Sub7.Option ExplicitOption Base 1Dim a(5, 5) As IntegerPrivate Sub Command1_Click() '生成数组并显示Dim i As Integer, j As IntegerPicture1.ClsPicture2.ClsPicture3.ClsRandomizeFor i = 1 To 5For j = 1 To 5a(i, j) = Int(90 * Rnd) + 10Picture1.Print a(i, j);NextPicture1.PrintNextEnd SubPrivate Sub Command2_Click()Dim i As Integer, j As Integer, max As IntegerDim b(5) As Integer, c(5) As IntegerFor i = 1 To 5max = a(i, 1)For j = 1 To 5If max < a(i, j) Thenmax = a(i, j)End Ifb(i) = maxIf max = a(i, j) Then c(i) = jNext jNext iFor i = 1 To 5Picture2.Print b(i)Picture3.Print c(i)NextEnd Sub8.Option ExplicitOption Base 1Dim a(5, 5) As IntegerPrivate Sub Command1_Click() '生成数组并显示Dim i As Integer, j As IntegerPicture1.ClsPicture2.ClsLabel3.Caption = "最大值?"Label4.Caption = "最小值?"RandomizeFor i = 1 To 5For j = 1 To 5a(i, j) = Int(90 * Rnd) + 10Picture1.Print a(i, j);NextPicture1.PrintNextEnd SubPrivate Sub Command2_Click() '交换并显示Dim i As Integer, j As Integer, t As IntegerDim maxi As Integer, mini As Integer, max As Integer, min As Integer max = 10: min = 99For i = 1 To 5For j = 1 To 5If max < a(i, j) Thenmax = a(i, j)maxi = iEnd IfIf min > a(i, j) Thenmin = a(i, j)mini = iEnd IfNextNextIf maxi <> mini ThenFor j = 1 To 5t = a(maxi, j)a(maxi, j) = a(mini, j)a(mini, j) = tNext jFor i = 1 To 5For j = 1 To 5Picture2.Print a(i, j);NextPicture2.PrintNextLabel3.Caption = "最大值" & max & "在第" & maxi & "行"Label4.Caption = "最小值" & min & "在第" & mini & "行"ElsePicture2.Print "最小值和最大值在一行"Label3.Caption = "最大值" & max & "在第" & maxi & "行"Label4.Caption = "最小值" & min & "在第" & mini & "行"End IfEnd SubPrivate Sub Form_click()Picture1.ClsPicture2.ClsLabel3.Caption = ""Label4.Caption = ""End Sub9.Option Base 1Private Sub Command1_Click()Dim i As Integer, j As Integer, n As Integer, a() As Integern = InputBox("请输入一个奇数N:")If n Mod 2 = 0 ThenMsgBox ("请重新输入奇数!")End IfReDim a(n, n)For i = 1 To nFor j = 1 To nIf i = (n + 1) / 2 And j = (n + 1) / 2 Thena(i, j) = -1ElseIf i = j Or i + j = n + 1 Thena(i, j) = 1Elsea(i, j) = 0End IfNext jFor i = 1 To nFor j = 1 To nPrint a(i, j);Next jPrintNext iEnd Sub10.Option Base 1Dim a(10) As IntegerPrivate Sub Command1_Click()Dim i As IntegerPrint "生成的数组:"RandomizeFor i = 1 To 10a(i) = Int(Rnd * 90) + 10Print a(i);NextPrintEnd SubPrivate Sub Command2_Click()Dim i As Integerx = InputBox("请输入要删除的数组元素下标:") Print "删除第" & x & "个数组元素后:"For i = x + 1 To 10a(i - 1) = a(i)NextFor i = 1 To 9Print a(i);NextEnd Sub11.Option ExplicitDim a(20) As Integer, i As IntegerPrivate Sub Command1_Click()RandomizeFor i = 1 To 20a(i) = Int(Rnd * 9) + 1Text1 = Text1 & a(i) & " "If i = 10 Then Text1 = Text1 & vbCrLf NextPrivate Sub Command2_Click()Dim j As Integer, k As Integer, b(20) As Boolean 'b(2)用来记录a(2)是否和前面的数相同,相同记为true,不再统计For i = 1 To 20k = 1If b(i) = False Then '如果b(i)=False,表示a(i)没有被统计过For j = i + 1 To 20If a(i) = a(j) Thenk = k + 1b(j) = TrueEnd IfNext jList1.AddItem a(i) & "出现次数:" & kEnd IfNextEnd SubPrivate Sub Command3_Click()Text1 = ""List1.ClearEnd Sub第6章过程一、选择题二、填空题1. 6 .2. 4 20 1013.第一行是 6 26 、第三行是-2 984.第一行10 6 、第二行 5 -55.第一行 6 5 、第三行8 76.第一行11 、第二行77.Fun(i)Fun=p8.第一行 4 、第二行2079.第一行29 、第二行28.710.J=kb( ) As Integerb( k-1) =b(k)i11.第一行gec 、第二行ge 、第四行 312.p1 = True And p2 = True 或p1 And p2= isp(i)Isp = Truem Mod i = 013.Convert(st)Len(s)"A" To "F"k = k + p * h14.isualVisualVisu15.s + an + 1x As Single, n As Integer 、p * (x + i) / ((2 * i - 1) * i * x)16.27617.Left(s, 1)Mid(s, i - 1, 1) = Mid(s, i, 1)n = 1c & CStr(n)18.chtempTrue19.val(mid(s,i,j))n as longprime=True20.A(2,2)的值是 6 ,A(3,1) 的值是8 ;A(4,2) 的值是 321.Option1(i)X22.1023.第一行7 6 ,第二行16 16 ,第三行,4524.第一行15 1 ,第二行35 3 ,第三行,50三、改错题1、Option Base 1Private Sub Command1_Click()Dim A() As Integer, I As Integer, J As Integer, Logic As Boolean Dim K As IntegerReDim A(1)A(1) = 50————————'K=1For I = 51 To 149K = 1 '位置错Logic = FalseCall Sub1(A, I, Logic)If Logic ThenK = K + 1ReDim A(K) 'ReDim Preserve A(K)A(K) = IEnd IfNext IFor I = 1 To UBound(A)Text1 = Text1 & Str$(A(I))If I Mod 5 = 0 Then Text1 = Text1 & vbCrLfNext IEnd SubPrivate Sub Sub1(A() As Integer, N As Integer, F As Boolean) Dim I As Integer, J As Integer, Ub As IntegerUb = UBound(A)For I = 1 To UbFor J = 2 To A(I)If A(I) Mod J = 0 And N Mod J = 0 ThenExit For 'Exit SubEnd IfNext JF = TrueNext IEnd Sub2、Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As IntegerDim J As Integer, K As Integer, P() As IntegerReDim P(1)P(1) = 2For I = 3 To 150 Step 2For J = 2 To Sqr(I)If I Mod J = 0 Then Exit ForNext JIf J > Sqr(I) ThenReDim Preserve P(UBound(P) + 1)P(UBound(P) + 1) = I 'P(UBound(P)) = IEnd IfNext IFor I = 80 To 125If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)Text1 = Text1 & Str$(I) & "=" & Str(S1) & "*" & Str$(S2) & vbCrLf End IfNext IEnd SubPrivate Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As IntegerFor I = 1 To UBound(P)For J = 1 To UBound(P) - 1If N = P(I) * P(J) ThenS1 = P(I)S2 = P(J)'fun = TrueExit Functionfun = True '位置错End IfNext JNext IEnd Function3、Option ExplicitPrivate Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByVal idx As IntegerDim Ub As Integer, Counter As Integer, K As IntegerUb = UBound(Chess)Chess(idx) = 0Counter = 1K = 0 '位置错Do Until Counter = Ub - 1Do Until K = 2idx = idx + 1If idx > Ub Then idx = 1K = K + Chess(idx)LoopChess(idx) = 0Counter = Counter + 1LoopFor Over = 1 To UbIf Chess(Over) <> 0 Then Exit ForNext OverEnd SubPrivate Sub Command1_Click()Dim Chess(32) As Integer, I As Integer, J As IntegerDim K As Integer, idx As IntegerFor I = 1 To 32For J = 1 To 32Chess(J) = 1Next JCall Get_Chess(Chess, I, K)If K = 23 ThenText1 = "从第" & Str$(I) & "号棋子开始取"Exit ForEnd IfNext IEnd Sub4、Option ExplicitPrivate Sub Form_Click()Dim S As String, Flg As BooleanDoS = InputBox("输入一个自然数")If S = "" Or Not IsNumeric(S) ThenMsgBox "请重新输入", 48 + vbOKOnlyElseExit DoEnd IfLoopjudge(S, Flg) ' Call judge(S, Flg) 或judge S, Flg If Flg ThenPrint S; "是回文数"ElsePrint S; "不是回文数"End IfEnd SubPrivate Sub judge(Ch As String, F As Boolean)Dim L As Integer, I As IntegerL = Len(Ch)F = True ' F = FalseFor I = 1 To LIf Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next IF = False ' F = TrueEnd Sub四、编程题1.Private Function Prime(a As Integer) As BooleanDim i As IntegerFor i = 2 To Sqr(a)If a Mod i = 0 Then Exit FunctionNext iPrime = TrueEnd Function'方法一Private Sub Command1_Click()Dim m As Integer, i As Integerm = InputBox("请输入一个大于2 的偶数")If m <= 2 Or m Mod 2 <> 0 Then MsgBox "请输入大于2的偶数": Exit Sub If Prime(2) And Prime(m - 2) ThenPrint m & "=2 +" & m - 2ElseDoi = i + 1If Prime(2 * i + 1) And Prime(m - (2 * i + 1)) ThenPrint m & "=" & 2 * i + 1 & "+" & m - (2 * i + 1)Exit DoEnd IfLoop While 2 * i + 1 < m - (2 * i + 1)End If'方法二Private Sub Command2_Click()Dim m As Integer, i As Integer, p() As Integer, n As IntegerDim flg As Booleanm = InputBox("请输入大于2的偶数")If m <= 2 Or m Mod 2 <> 0 Then MsgBox "请输入大于2的偶数": Exit Sub ReDim p(1)p(1) = 2n = 1For i = 3 To m - 2 Step 2If Prime(i) Thenn = n + 1ReDim Preserve p(n)p(n) = iEnd IfNext iFor i = 1 To nFor j = 1 To nIf p(i) + p(j) = m ThenPrint m & "=" & p(i) & "+" & p(j)flg = TrueExit ForEnd IfNext jIf flg = True Then Exit ForNext iEnd Sub2.Private Function Fact(a As Integer) As LongIf a = 0 Or a = 1 ThenFact = 1ElseFact = a * Fact(a - 1)End IfEnd FunctionPrivate Sub Command1_Click()Dim n As Integer, m As Integer, result As Singlen = Val(Text1.Text)m = Val(Text2.Text)If n >= m Thenresult = Fact(n) / Fact(m) / Fact(n - m)Text3.Text = resultMsgBox "要求n>=m,请重新输入"Text1.Text = ""Text2.Text = ""Text1.SetFocusEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub3.Private Function Istgs(n As Integer) As BooleanDim s As String, L As IntegerL = Len(CStr(n))s = CStr(n ^ 2)If Right(s, L) = n Then Istgs = TrueEnd FunctionPrivate Sub Command1_Click()Dim n As IntegerFor n = 10 To 300If Istgs(n) Then List1.AddItem n & "^2" & "=" & n ^ 2 Next nEnd SubPrivate Sub Command2_Click()EndEnd Sub4.Private Sub Command1_Click()Dim a(3, 4) As Integer, i As Integer, j As IntegerDim m As Integer, mj As IntegerRandomizeFor i = 1 To 3For j = 1 To 4a(i, j) = Int(900 * Rnd + 100)Print a(i, j);Next jPrintNext iFor i = 1 To 3m = 0Call max(a, i, m, mj)Print "第" & i & "行最大值为" & m & ",在第" & mj & "列"Next iEnd SubPrivate Sub max(a() As Integer, i As Integer, m As Integer, maxj As Integer) Dim j As Integerm = a(i, 1): maxj = 1For j = 2 To UBound(a, 2)If a(i, j) > m Thenm = a(i, j)maxj = jEnd IfNext jEnd SubPrivate Sub Command2_Click()EndEnd Sub5.Private Function Judge(n As Integer) As BooleanDim sum As Integersum = yz(n)If yz(sum) = 2 * n Then Judge = TrueEnd FunctionPrivate Function yz(n As Integer) As IntegerDim i As IntegerFor i = 1 To nIf n Mod i = 0 Then yz = yz + iNext iEnd FunctionPrivate Sub Command1_Click()Dim n As IntegerFor n = 1 To 500If Judge(n) Then Print nNext nEnd SubPrivate Sub Command2_Click()EndEnd Sub6.Private Sub Command1_Click()Dim n As Integer, j As Integer, flg As BooleanFor n = 2 To 30flg = FalseFor j = 2 To nIf pfs(j) And n Mod j = 0 Thenflg = TrueExit ForEnd IfNext jIf flg = False Then Print n;Next nEnd SubPrivate Function pfs(n As Integer) As BooleanIf Sqr(n) = Int(Sqr(n)) Then pfs = TrueEnd Function7.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd Function'判断某数是否为回文数Private Function Hws(n As Long) As BooleanDim i As Integer, s As String, L As Integers = CStr(n): L = Len(s)For i = 1 To Len(s) / 2If Mid(s, i, 1) <> Mid(s, L + 1 - i, 1) Then Exit Function Next iHws = TrueEnd FunctionPrivate Sub Command1_Click()Dim x As Long, t As Longx = InputBox("请输入一个正整数:")t = xDo While Hws(t) = FalsePrint t; "+"; fxs(t); "="; t + fxs(t)t = t + fxs(t)LoopEnd Sub8.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd FunctionPrivate Sub Command1_Click()Dim n As Long, t As LongFor n = 1 To 999t = fxs(n)If t ^ 2 = fxs(n ^ 2) And n Mod 10 <> 0 And n < t ThenList1.AddItem n & " " & tList2.AddItem n & "^2" & "=" & n ^ 2 & " " & t & "^2" & "=" & t ^ 2 End IfNext nEnd Sub9.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd FunctionPrivate Function Judge(n As Integer) As BooleanDim i As Integer, L As Integer, a(9) As Integer, t As IntegerIf Sqr(n) = Int(Sqr(n)) ThenL = Len(CStr(n))For i = 1 To Lt = Mid(CStr(n), i, 1)a(t) = 1Next iFor i = 0 To 9Sum = Sum + a(i)Next iIf Sum = L - 1 Then Judge = TrueEnd IfEnd FunctionPrivate Sub Command1_Click()Dim a As IntegerFor a = 300 To 2000If Judge(a) Then List1.AddItem aNext aEnd Sub10.Private Function F(n As Integer) As BooleanDim i As IntegerFor i = 1 To Len(CStr(n))If i Mod 2 = 1 And Mid(CStr(n), i, 1) Mod 2 <> 1 Then Exit FunctionIf i Mod 2 = 0 And Mid(CStr(n), i, 1) Mod 2 <> 0 Then Exit Function Next iF = TrueEnd FunctionPrivate Sub Command1_Click()Dim a(8) As Integer, i As Integer, j As IntegerRandomizeDot = Int(900 * Rnd + 100)For j = 1 To iIf t = a(j) Then Exit ForNext jIf j = i + 1 Theni = i + 1a(i) = tList1.AddItem a(i)If F(a(i)) Then List2.AddItem a(i)End IfLoop Until i = 8End SubPrivate Sub Command2_Click()List1.ClearList2.ClearEnd SubPrivate Sub CmdEnd_Click()EndEnd Sub11.Private Function prime(a As Integer) As BooleanDim i As IntegerFor i = 2 To Sqr(a)If a Mod i = 0 Then Exit FunctionNext iprime = TrueEnd FunctionPrivate Function nx(n As Integer) As IntegerDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext inx = Val(s)End FunctionPrivate Sub Command1_Click()Dim n As Integer, t As IntegerFor n = 100 To 900t = nx(n)If prime(n) And prime(t) And n < t ThenList1.AddItem "(" & n & "," & t & ")"End IfNext nEnd SubPrivate Sub Command2_Click()List1.ClearEnd Sub12.Option Base 1Dim a(4, 5) As IntegerPrivate Sub Command1_Click()Dim i As Integer, j As IntegerRandomizeFor i = 1 To 4For j = 1 To 5a(i, j) = Int(Rnd * 90 + 10)Picture1.Print a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Fp(a() As Integer, i As Integer, maxj As Integer, Flg As Boolean) Dim max As Integer, j As Integer, t As Integermax = a(i, 1): maxj = 1For j = 2 To UBound(a, 2)If a(i, j) > max Thenmax = a(i, j)maxj = jEnd IfNext jFor t = 1 To UBound(a, 1)If a(t, maxj) > max ThenFlg = FalseExit SubEnd IfNextFlg = TrueEnd SubPrivate Sub Command2_Click()Dim i As Integer, m As Integer, mj As Integer, f As BooleanFor i = 1 To 4f = Falsem = 0: mj = 0Call Fp(a, i, mj, f)If f = True ThenText1.Text = Text1.Text & "(" & i & "," & mj & "):" & a(i, mj) & vbCrLf End IfNext iEnd SubPrivate Sub Command3_Click()Picture1.ClsText1 = ""End Sub13.Private Sub Command1_Click()Dim a(30) As Integer, i As Integer, j As IntegerDim Flag As BooleanRandomizeDot = Int(900 * Rnd + 100)For j = 1 To iIf t = a(j) Then Exit ForNext jIf j = i + 1 Theni = i + 1a(i) = tText1.Text = Text1.Text + Str(a(i))If i Mod 10 = 0 Then Text1.Text = Text1.Text + vbCrLfIf IsDown(a(i)) ThenList1.AddItem a(i)Flag = TrueEnd IfEnd IfLoop Until i = 30If Flag = False Then List1.AddItem "无降序数"End SubPrivate Function IsDown(n As Integer) As BooleanDim i As Integer, S As StringS = CStr(n)For i = 1 To Len(S) - 1If Mid(S, i, 1) < Mid(S, i + 1, 1) Then Exit FunctionNext iIsDown = TrueEnd FunctionPrivate Sub Command2_Click()List1.ClearText1 = ""End Sub14.Option ExplicitPrivate Sub CmdFind_Click()Dim i As Integer, p As String, st As String, st1 As StringFor i = 100 To 999st = ""If csum(i) = zyz(i, st) Thenp = CStr(i)st1 = Left(p, 1) & "+" & Mid(p, 2, 1) & "+" & Right(p, 1)List1.AddItem i & ": " & st1 & "=" & Left(st, Len(st) - 1)End IfNext iIf List1.ListCount = 0 ThenList1.AddItem "无满足要求的数!"End IfEnd SubPrivate Function csum(m As Integer) As Integer '求各位数字之和Dim i As Integer, p As Stringp = CStr(m)For i = 1 To Len(p)csum = csum + Val(Mid(p, i, 1))Next iEnd FunctionPrivate Function zyz(ByVal m As Integer, st As String) As Integer '求所有质因子之和Dim i As Integeri = 2DoIf m Mod i = 0 Thenzyz = zyz + ist = st & i & "+"m = m \ iElsei = i + 1End IfLoop Until m = 1End Function15.Private Sub Command1_Click()Dim k As Integer, i As Integerk = InputBox("请输入要求的数列项数:")List1.AddItem "共输出前" & k & "项数列"For i = 0 To kList1.AddItem p(i)NextEnd SubPrivate Function p(n As Integer) As IntegerIf n = 0 Or n = 1 Or n = 2 Thenp = 1Elsep = p(n - 2) + p(n - 3)End IfEnd Function第7章文件一、选择题二、填空题1.顺序文件和随机文件2. EOF3. Write和Print4.返回或设置当前的工作目录5.5116.文件的写语句7.Append8.LOF9.关闭10.Pattern11.【1】For Input 【2】#2 【3】Not EOF(2) 12.【1】”END”【2】Text1.Text 或者Text113.【1】Input 【2】ch 【3】Len(mystr) 14.【1】Number 【2】s15.【1】For Append As #1 【2】"end" 【3】ct,nt 16.【1】For Input 【2】For Output As #2 【3】Not EOF(1) 【4】Line Input #1,str1 【5】Close #1,#2 【6】Kill “C:\old.txt 17.【1】Open "C:StuData.txt" For OutPut As #1 【2】Write 18.【1】"C:\data.txt" 【2】#1, a(i) 【3】k>1 And a(k-1)>b 【4】k = k -1三、综合题1.Option Base 1Dim arr(100) As IntegerPrivate Sub Command1_Click()Dim i As IntegerOpen "c:\in18.txt" For Input As #6For i = 1 To 100Input #6, arr(i)Next iClose #6End SubPrivate Sub Command2_Click()Dim i As IntegerText1.Text = ""Open "c:\out18.txt" For Output As #8For i = 1 To 100If arr(i) Mod 2 <> 0 ThenText1.Text = Text1.Text & Str(arr(i))Write #8, arr(i)End IfNext iClose #8End Sub2.Dim str As StringDim n As LongPrivate Sub Command1_Click()CommonDialog1.Filter = "文本文件(*.txt)|*.txt|All Files(*.*)|*.*"CommonDialog1.FilterIndex = 1CommonDialog1.Action = 1Open CommonDialog1.FileName For Input As #2n = LOF(2)str = Input(n, #2)Text1.Text = strClose #2End SubPrivate Sub Command2_Click()Dim i As IntegerFor i = 1 To Len(str)If Asc(Mid(str, i, 1)) <= Asc("Z") And Asc(Mid(str, i, 1)) >= Asc("A") Then Mid(str, i, 1) = LCase(Mid(str, i, 1))ElseIf Asc(Mid(str, i, 1)) <= Asc("z") And Asc(Mid(str, i, 1)) >= Asc("a") Then Mid(str, i, 1) = UCase(Mid(str, i, 1))End IfNext iText1.Text = strEnd SubPrivate Sub Command3_Click()Open "D:\out.txt" For Output As #3Print #3, strClose #3End Sub第8章程序调试一.选择题二.改错题1.a(i)=int(Rnd*19)+1 →a(i)=int(rnd*20)+1For l=1 to a(i)+1 →for i=2 to a(i)+1If l=a(i) →if l <> a(i)K=k+1 →k=k-1。

VB程序设计学习与实验指导书答案(包含实验答案)

VB程序设计学习与实验指导书答案(包含实验答案)

VB学习与实验指导测试题参考答案第一章第二章第三章第四章第五章第六章第七章第八章第九章综合测试题一综合测试题二下篇实验教程实验2 Visual Basic 变量、函数与表达式的使用实验2-1 算术运算符的使用13178332262-2-22007-10-153 2.857143 2.85714285714286实验2-2 字符运算符的使用20071020071020172007-10-10102007-10-20出错,类型不匹配实验2-3 关系运算符的使用FalseFalseFalseFalseFalseFalseFalseTrueFalseTrue实验2-4 逻辑运算符的使用TrueTrueFalseTrueFalseFalseTrueTrueFalse5-59实验2-5 常用函数的使用3.14 3.147.389056098930651 -1 .999999998205103 4-1 1 0.70554755825 251250 0A a65 9748 688 -98 -89 -9aaa 4.5 aaa4.516 5Visua 6.0sua Basic Basic 6.0abcde EFGBasic Basic BasicVisual Basic 6.0 Basic8 8aaa dd2011-3-152011-3-15 14:35:5215320113001.731.731.7317.32E-010173%实验3 算法基础及程序控制结构实验3-1 三个数的交换A=CC=BB=TBC实验3-2 求周长和面积Const PI=3.14159262*PI*RPI*R*RPicture1.ClsFormat(L, ".00")Format(M, ".00")实验3-3 求分段函数的值x=Val(Text1.Text)y=1+Sin(x)y=Log(x)Text1.Text = ""Text2.Text = ""实验3-4 求一元二次方程的根Val(Text2.Text)Val(Text3.Text)b ^ 2 - 4 * a * c"x1=" & (-b + Sqr(delt)) / (2 * a)"x2=" & (-b - Sqr(delt)) / (2 * a)Text2.Text = ""Text3.Text = ""End实验3-5 判断素数N Mod k = 0k=NN Mod i=0k=2实验3-6 求最大公约数和最小公倍数M<NL=ML=NM Mod k = 0 And N Mod k = 0Text3.Text = kExit ForM>NL=ML=Nk Mod M = 0 And k Mod N = 0Text4.Text = kExit For实验3-7 判断升序数、降序数CStr(N)Len(S) – 1Mid(S, k, 1) >= Mid(S, k + 1, 1)k = Len(S)N = Val(Text1.Text)S = CStr(N)For k = 1 To Len(S) - 1If Mid(S, k, 1) <= Mid(S, k + 1, 1) Then Exit For Next kk = Len(S)实验3-8 判断回文数1Len(s)\2-1Mid(S, k, 1) <> Mid(S, Len(S) - k + 1, 1)k = Len(S) \ 2Mid(S, k, 1) + S1S = S1实验3-9 求级数的值Val(Text1.Text)1x ^ ns * nt / s <= 10 ^ -6Text2.Text = y实验3-10 查找数字串并求和Text1.Text = ""Text2.Text = ""List1.ClearText1.TextFalseWhile Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Flag = Truek = k * 10 + Mid(s, i, 1)i = i + 1Flag = Truesum + kText2.Text = sum实验3-11 随机产生20个奇数Int((99 - 10 + 1) * Rnd) + 10a Mod 2 = 1n Mod 5 = 0Picture1.Cls实验3-12 加密解密Chr(Asc("a") + (Asc(c) - Asc("a") + 3) Mod 26)c = Chr(Asc("A") + (Asc(c) - Asc("A") + 3) Mod 26) c & s2Text2.TextLen(s2)Mid(s2, i, 1)c = Chr(Asc("a") + (Asc(c) - Asc("a") + 23) Mod 26) c = Chr(Asc("A") + (Asc(c) - Asc("A") + 23) Mod 26) c & s1s1Text1.Text = ""Text2.Text = ""实验3-13 统计单词个数Text1.TextMid(s, i, 1) <> " " And i <= Len(s)i = i + 1Mid(s, start, i - start)nText1.Text = ""List1.Clear实验4 数组的使用实验4-1 一维数组的产生及输出i = 1 To 20Int((9 - 0 + 1) * Rnd + 0)i = 1 To 20Print a(i);Printi = 1 To 20Picture1.Print a(i);If i Mod 5 = 0 Then Picture1.Printi = 1 To 20Text1.Text & Space(2) & a(i)If i Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf v In aIf i Mod 5 = 0 Then Picture2.Print实验4-2 选手得分Int((50 - 10 + 1) * Rnd + 10) / 10i = 1 To 6sum = sum + score(i)max = score(1)min = score(1)score(i) > max Then max = score(i)score(i) < min Then min = score(i)(sum - max - min) / 4实验4-3 产生10个互不相同的整数Int((999 - 10 + 1) * Rnd + 10)t Mod 10 = 5Then Exit Forn= n + 1a(i) = tFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext i实验4-4 统计数字出现的次数IsNumeric(S) = FalseExit SubLen(S)c >= "0" And c <= "9"A(c) = A(c) + 1A(k) <> 0m = m + 1实验4-5 排序1 To 10Int((99 - 10 + 1) * Rnd + 10)Text1.Text & A(n) & " "1 To 9i + 1 To 10A(i) > A(j)T = A(j)A(j) = A(i)A(i) = T1 To 910 - iA(j) > A(j + 1)T = A(j + 1)A(j + 1) = A(j)A(j) = T1 To 10Text3.Text = Text3.Text & A(i) & " "10T = A(i)ji - 1k + 11 To 10Text4.Text = Text4.Text & A(i) & " "实验4-6 二维数组的产生及输出1 To 41 To 5Int((9 - 0 + 1) * Rnd + 0)1 To 41 To 5Print A(i, j);Print1 To 41 To 5Picture1.Print A(i, j);Picture1.Print1 To 41 To 5Text1.Text = Text1.Text & A(i, j) & " "Text1.Text = Text1.Text & vbCrLf实验4-6 查找最大和最小元素及其位置1 To 31 To 4A(i, j) = Int((99 - 10 + 1) * Rnd + 10)Picture1.Print A(i, j);Picture1.PrintA(1, 1), 1, 1A(1, 1), 1, 1A(i, j)ijA(i, j) < minA(i, j)ij"最大元素" & "A(" & maxi & "," & maxj & ")=" & max "最小元素" & "A(" & mini & "," & minj & ")=" & min实验4-7 矩阵转置Dim A() As Integer"请输入N的值"ReDim A(N, N) As IntegerFor i = 1 To NFor j = 1 To NA(i, j) = Int((99 - 10 + 1) * Rnd + 10)Text1.Text = Text1.Text & A(i, j) & " "Next jText1.Text = Text1.Text & vbCrLfNext it = A(i, j)A(i, j) = A(j, i)A(j, i) = tFor i = 1 To NFor j = 1 To NText2.Text = Text2.Text & A(i, j) & " "Next jText2.Text = Text2.Text & vbCrLfNext i实验5 过程实验5-1 孪生素数和降序素数Dim i As IntegerFor i = 2 To N - 1If N Mod i = 0 Then Exit FunctionNext iPrime = TrueDim a As IntegerDim b As Integera = N \ 10b = N Mod 10If a > b ThenDecNumber = TrueElseDecNumber = FalseEnd If10 To 100Prime(i) = True And Prime(i + 2) = TrueList2.AddItem i & "和" & i + 2DecNumber(i) = True And Prime(i) = TrueList3.AddItem i实验5-2 求多项式和k = 1a = f(x, k)S + ak = k + 1Single As SingleDim i As Integer, a As Singlea = (x + 1) / xFor i = 2 To ka = a * (x + i) / ((2 * i - 1) * x)Next ia实验5-3 进制转换Asc(C) - Asc("A") + 10Left(S, k - 1)Mid(S, k + 1)Change(Mid(S2, i, 1)) * N ^ (0 - i)T1 + T2Tran(8, S)Tran(16, S)Not (C >= "0" And C <= "7" Or C >= "A" And C <= "F" Or C = ".")实验5-4 Armstrong数i As Long, a As Long, b As LongCombo1.Text实验5-5Option Base 1Dim a(5) As IntegerDim b(5) As Integerb(i) = i * i + 1Text2.Text = Text2.Text & b(i) & " "1 To 51 To 5Gcd(a(i), b(j)) = 1Gcd = KExit For实验5-6Create(a)Output(a)Sort(a)Output(a)GetMaxMin(a, MaxData, MinData)MaxDataMinDataAverage(a)Int((99 - 10 + 1) * Rnd + 10)1 To UBound(a)a(i);a(i) > a(j)t = a(j)a(j) = a(i)a(i) = t2 To UBound(a)a(i) > MM = a(i)a(i) < NN = a(i)s = s + a(i)Average = s / UBound(a)实验6 界面设计实验6-1 用户登陆MsgBox "请登录!"PassWordInput=TxtPassW.TextUserIdInput=UserId And PassWordInput=PassWordCmdReset_Click实验6-2 学籍登记OptXb(0).Value = TrueList1.AddItem Space(4) & xh & Space(6) & xm & Space(6) & xb & Space(12) & xy & Space(16) & zy & Space(4)List1.RemoveItem List1.ListIndex实验6-3 字体格式设置Select Case OpFontCheck2.Value = 1OpColor = IndexOpFont = Index实验6-4 兴趣调查List1.AddItem ItemEndSelect Case IndexItem = Text1.Text实验6-5 调色板。

vb实验报告标准答案

vb实验报告标准答案

竭诚为您提供优质文档/双击可除vb实验报告标准答案篇一:Vb郭贤海实验报告及答案实验1.1设计一个程序,程序运行的初始界面如图1-1所示。

当单击“显示”按钮时,在文本框中显示“欢迎使用Vb!”(图1-1);当单击“清除”按钮时,清除文本框内的文本;当单击“退出”按钮时,结束程序运行。

图1-1程序运行初始界面图1-2单击“显示”按钮后的程序界面实验1.2设计一个程序,程序运行的初始界面如图1-3所示,其中“显示”按钮为不可用。

同时要求:(1)当单击“改变文字”按钮时,在左边标签文字内容改为“你好,张三!”,同时“改变文字”按钮变为不可用(图1-4);(2)当单击“隐藏”按钮时,将左边标签设为不可见,同时“隐藏”按钮变为不可用,“显示”按钮变为可用(图1-5);(3)当单击“显示”按钮时,重新显示左边标签,同时“隐藏”按钮变为可用,“显示”按钮变为不可用(图1-4)。

图1-3第1题的程序运行初始界面图1-4单击“改变文字”按钮后的程序界面图1-5单击“隐藏”按钮后的程序界面四、实验原始记录实验1.1程序的界面设计。

(可以是多张主要的图。

下面的图要替换成你们自己设计的图。

如何取图?设计好界面,按“printscreensysRq”键,打开画图软件,单击“编辑|粘贴”命令,利用菜单栏下方的“选定”功能,选中窗体,按“ctrl+c”键,然后选中下面的实验样例图,按“ctrl+V”键,替换掉)程序代码。

privatesubcommand1_click()Text1.Text="欢迎使用Vb!"endsubprivatesubcommand2_click()Text1.Text=""endsubprivatesubcommand3_click()endendsub实验1.2程序的界面设计。

(可以是多张主要的图。

下面的图要替换成你们自己设计的图)程序代码。

privatesubcommand1_click()Label1.Visible=Truecommand2.enabled=Truecommand1.enabled=Falseendsubprivatesubcommand2_click()Label1.Visible=Falsecomma nd2.enabled=Falsecommand1.enabled=Trueendsub privatesubcommand3_click()Label1.caption="你好,张三!"command3.enabled=Falseendsub五、实验结果及分析实验1.1运行结果。

VB实验操作题集连学习答案

VB实验操作题集连学习答案

操作题一1. 在学生文件夹中有工程文件sj126.vbp 其中的窗体如图所示。

程序刚运行时,会生成一个有10个元素的整型数组。

若选中"查找最大值"(或"查找最小值")单选按钮,再单击"查找"按钮,则找出数组中的最大值(或最小值)并显示在标签Label2中。

请去掉程序中的注释符, 把程序中的? 改为正确的内容。

注意:学生不得修改窗体文件中已经存在的程序。

最后把修改后的文件按原文件名存盘。

Private Sub Command1_Click()Dim A(1 To 10) As IntegerDim i As Long, s As StringFor i = 1 To 10A(i) = Int(Rnd * 1000)Next ix = A(1)y = A(1)For i = 2 To 10If Option1.Value = True ThenIf A(i) > x Thenx = A(i)End IfElseIf A(i) < y Theny = A(i)End IfNextIf Option1.Value = True ThenLabel2.Caption = xElseLabel2.Caption = yEnd IfEnd Sub2. 在名称为Form1的窗体上画一个名称Shape1的形状控件,在属性窗口中将其设置为圆形。

画一个名称为List1的列表框,并在属性窗口中设置列表项的值分别为1、2、3、4、5。

将窗体的标题设为"图形控件"。

单击列表框中的某一项,则将所选的值作为形状控件的填充参数。

例如 选择3 则形状控件中被竖线填充。

如图所示。

要求:程序中不得使用变量,每个事件过程中只能写一条语句。

存盘时放在学生文件夹下, 工程文件名为sj107.vbp 窗体文件名为sj107.frm。

在Shape控件中,在Shape中选3-circle, 在List1的列表框中List输入值分别为1、2、3、4、5 。

VB实验作业参考答案

VB实验作业参考答案
2.编程题:
1)单击窗体后在窗体上显示“您好!×××同学”。
要求:(1)程序中用自己的姓名替代“×××”。(2)文字以红色、仿宋体、加粗、三号字显示,并在字下加横线。(3)建立一个命令按钮,结束时单击此按钮退出。
答案参照课本P27的例1-6。
2)设计一窗体,窗体上有1个文本框(Text1)、1个命令按钮(Command1),运行时文本框中显示“Visual Basic程序设计”,命令按钮标题为“结束”,文本框及命令按钮能随窗体大小的调整而自动调整大小及位置,其中调整文本框使其:Left=0,Top=0,宽度和高度都为窗体的一半;命令按钮:始终位于窗体右下角位置。请编写:
Command1.Top = Form1.ScaleHeight - Command1.Height
End Sub
Private Sub Form_Resize()
Call Form_Load
End Sub
Private Sub Command1_Click()
End
Dim r1 As Single, r2 As Single, Rp As Single, Rs As Single
r1 = InputBox("请输入第一个电阻值:")
r2 = InputBox("请输入第二个电阻值:")
Rp = (r1 * r2) / (r1 + r2)
Rs = r1 + r2
程序代码:
Private Sub Command1_Click()
Dim sg As String, tz As String, bz As String
sg = Text1.Text

【参考文档】vb实验指导书答案-范文word版 (10页)

【参考文档】vb实验指导书答案-范文word版 (10页)
3
Dim b As Single Dim c As Single Dim y As Single a = 5 b = 2.5 c = 7.8
y = (a * b * 3.14) / (a + b * c) Print y End Sub
2. 参考代码如下:
Private Sub Form_Click() Dim a As Single Dim b As Single Dim c As Single Dim d As Single
本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!
== 本文为word格式,下载后可方便编辑和修改! ==
vb实验指导书答案
篇一:VB程序设计学习与实验指导书答案(包含实验答案)
VB学习与实验指导测试题
参考答案
第一章
第二章
第三章
第四章
第五章
第六章
第七章
第八章
篇二:VB实验指导书答案
A
14. C 15. C 16. D 17. B 18. A 19. C 20. A 21. A 22. B 23. D 24. C 25. C 26. B 27. D 28. A 29. B 30. A 31. D 32. C
33. D 34. D 35. A 36. A 37. A 38. D 39. B 40. B 41. B 42. B 43. C 44. C 45. C 46. B 47. B 48.
2
4. sq(x As Integer),Function 5. Val(t.Text) 6. 30,70 7. 5
9. 30
10. nAA N, X ,N1,nT1 * X1
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验教程
实验三顺序结构程序设计编程题参考答案(第47页)
1. 输入学生的语文、数学、英语、物理四门课程的成绩,计算该学生的总成绩和平均成绩并输出。

要求使用InputBox分别进行输入,使用Print 方法输出。

2. 输入圆的半径,输出其周长、面积。

要求使用TextBox控件进行输入,使用Label控件输出。

3. 输入一元二次方程ax2+bx+c=0 的系数a、b、c,输出两个实根。

要求使用TextBox控件分别进行输入,使用Label控件输出。

实验四选择结构程序设计编程题参考答案(第58页)
1. 输入一个,判断其是否能同时被3和7整除。

2. 输入三个数,输出其中的最大值。

6. 编程及输出分段函数的值。

由键盘输入t的值,计算y的值
7.
B;20~29
为青年,输出B;
50
8. 输入年份,输出对应的属相。

已知1984年为鼠年,要求使用Select Case语句实现。

实验五循环结构程序设计编程题参考答案(第77页)
1.编写程序,求s=1×2×3 + 2×3×4 + … + n×(n+1) ×(n+2)的值,n由程序输入。

Y=
2.有一个分数序列
,求出这个序列前
20项的和。

3. 求∑=
10
1
!
n
n
(即1!+2!+3!+…+10!)
4. 求一类三位数,该书中有某两位为相同数字,且该数是一个完全平方数,输出这些数并统计个数。

5. 输出所有的“水仙花数”。

“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

例如,371是一个水仙花数,因为371=33+73+13
6. 打印如图所示的乘法表
实验六定长数组的使用编程题参考答案(第96页)
1.定义一个包含10个数组元素的数组a,为其数组元素分别赋值为2、4、6、8、…20,并输出。

2. 输入十个评委的分数,去掉最高分和最低分后,计算最后的平均分并输出。

3.输入5个学生的姓名与一门课程的成绩,按成绩的升序输出姓名和成绩。

分别使用比较
,...
13
21
,
8
13
,
5
8
,
3
5
,
2
3
,
1
2
互换法、选择法和冒泡法实现。

4. 输出5×5的矩阵,除对角线上的元素为1外,其他数组元素的值均为0。

5. 输入5×5的矩阵,求出每列元素之和,并把和值最大的那一列上的各元素和第一列各元素进行对调。

7. 输入十个学生的三门课程的成绩,并进行如下操作。

(1) 计算并输出每门课程的平均分。

(2) 输出每门课程中的最高分与最低分。

(3) 计算每个学生的平均分。

(4) 按学生的平均分进行降序排序。

实验七 动态数组、控件数组与自定义类型的数组编程题参考答案 (第10页)
6. 自定义学生类型,包括学生姓名、学号和成绩,输入学生人数n ,并输入学生的信息,按成绩进行降序排序后输出。

程序界面设计如下图。

实验七 过程编程题参考答案 (第123页)
2.编写子过程Sub ArraySa(a() As Integer, sum As Single, aver As Single),该过程的功能是求一维数组a 的元素和与平均值,并通过参数sum 和aver 返回结果。

4.编程子过程Sub movearray(a() As Integer, lr As Boolean, n As Integer),该过
程的功能是使一维数组元素左移或者右移若干位。

其中参数a()是数组、lr参数指示位移的方向(True向左移,False向右移)、n参数确定移动的位数。

程序界面如下图
实验八函数过程编程题参考答案(第137页)
1.编写程序,利用Function函数计算下式的值。

实验十一 VB的常用控件编程题参考答案(第123页)
1.利用命令按钮控制改变文字的显示区域。

程序界面如图所示,单击按钮“更该区域”按钮,将使“标签练习”几个字依次在上下两个显示区域(两个标签中)显示。

2.利用命令按钮控制改变文字的显示颜色。

在上题基础上,添加一个按钮并将其Cption设为“更改颜色”,单击按钮,使标签的字体颜色依次按红、绿、蓝变化。

提示:设置标签的字体颜色可以用RGB函数。

例如:
Label1.ForeColor = RGB(255,0,0) ‘设置红色
Label1.ForeColor = RGB(0,255,0) ‘设置绿色
Label1.ForeColor = RGB(0,0,255) ‘设置蓝色。

相关文档
最新文档