VB程序设计练习题(2022年新) |
您所在的位置:网站首页 › vb程序游戏满分的代码 › VB程序设计练习题(2022年新) |
目录
前言 一、数组元素互换。 二、同构数 三、成绩排名 四、兴趣爱好 五、 矩阵元素最高频率 六、sin与cos值 七、(进阶)旋转表 八、找小于给定素数最大的三个素数 九、求数列前n项 十、 红绿灯 总结: 前言寒假到了,做了我之前发题的后,想必自己的水平也高了不少了吧,这边继续发布我之前练过VB练习题给大家练练。难度较大。 一、数组元素互换。某数组有20个元素,元素的值由键盘输入,要求将第1个元素与第20个元素互换,第2个元素与第19个元素互换......第10个元素与第11个元素互换。输入数组原来各元素的值和对换后各元素的值。程序界面如下图。 答案: Dim a(1 To 20) Private Sub Command1_Click() s = 0 For i = 1 To 20 s = s + 1 a(i) = Val(InputBox("输入元素")) Label1.Caption = Label1.Caption & a(i) If i >= 1 And i = 1 And i 0 And b > 0 And b > a Then For i = a To b For j = 1 To Len(c) If i ^ 2 Mod 10 ^ j = i Then List1.AddItem i & "^2" & "=" & i ^ 2 Exit For End If Next Next End If If a > 0 And b > 0 And a > b Then For i = b To a For j = 1 To Len(d) If i ^ 2 Mod 10 ^ j = i Then List1.AddItem i & "^2" & "=" & i ^ 2 Exit For End If Next Next End If End Sub Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" End Sub Private Sub Command3_Click() End End Sub 三、成绩排名点击窗体利用随机数输出以下图案,并且排序,算出总分。 答案: Private Sub Form_Click() Dim a(1 To 10, 1 To 3) Dim b(1 To 10) Dim s As String Dim t As String Form1.Cls Randomize Sum = 0 Print "成绩 1 成绩 2 成绩 3 总分 名次" For i = 1 To 10 For j = 1 To 3 a(i, j) = Int(Rnd * (99 - 50 + 1) + 50) Sum = Sum + a(i, j) s = s & a(i, j) & Space(7) Next b(i) = Space(1) & s & Sum s = "" Sum = 0 Next For i = 1 To 10 For j = 1 + i To 10 If Right(b(i), 3) < Right(b(j), 3) Then t = b(i) b(i) = b(j) b(j) = t End If Next Print b(i) & Space(5) & i Next End Sub 四、兴趣爱好设计如图所示界面,窗体上有两个标签,1 个文本框,1 个列表框和三个命令按钮。程序运行时,在文本框中输入文本,单击“添加”按钮,文本添加到列表框中,如果列表框中有相同的文本,则不再添加。单击“删除”按钮,选中的列表项从列表框中移去。单击“清除”按钮,文本框和列表框中内容清空。 答案: Private Sub Command1_Click() Dim i As Integer Dim f As Boolean f = False For i = 0 To List1.ListCount - 1 If List1.List(i) = Text1.Text Then f = True Exit For Else f = False End If Next If f = False Then List1.AddItem Text1.Text End If End Sub Private Sub Command2_Click() List1.RemoveItem List1.ListIndex End Sub Private Sub Command3_Click() List1.Clear Text1.Text = "" End Sub 五、 矩阵元素最高频率安装如下布局摆放控件,单击command1时text1中生成4*5的0~9随机数的矩阵,点击统计按钮,则在text2中统计出出现频率最多的数和在text3中显示出现最多的次数。点击退出,则退出程序。 答案: Dim a(1 To 4, 1 To 5) As Integer Private Sub Command1_Click() Text1.Text = "" Randomize For i = 1 To 4 For j = 1 To 5 a(i, j) = Int(Rnd * (9 - 0 + 1) + 0) Text1.Text = Text1.Text & a(i, j) & " " Next Text1.Text = Text1.Text & vbCrLf Next End Sub Private Sub Command2_Click() Text2.Text = "" Text3.Text = "" Dim b(0 To 9) For i = 1 To 4 For j = 1 To 5 For n = 0 To 9 If a(i, j) = n Then 这边这是定义了一个b数组计入产 b(n) = b(n) & n 生的数,b的长度就是次数, End If 思路2:b(n)=b(n)+1来判断最大值 Next 如何次数跟值都有 Next Next For n = 0 To 9 If Len(b(n)) > Max Then Max = Len(b(n)) 找出b数组中长度最长的数,也就是 End If 次数最多的数。, Next For n = 0 To 9 If Len(b(n)) = Max Then Text2.Text = Text2.Text & n & " " 在text2中显示 Text3.Text = Len(b(n)) 在text3中显示次数(也就是长度) End If Next End Sub Private Sub Command3_Click() End End Sub 六、sin与cos值要求:①利用属性窗口设置适当的属性,使 txtInput、txtResult中数据右对齐:②请编写适当的程序完成以下功能:在 txtInput中输入40(度数),选择一个单选按钮,单击“计算”按钮,则根据所选择的单选按钮,计算出相应的正弦、余弦值(保留3位小数,第4位截去,pi取3.14159),并显示在 txtResult中。 答案: Const pi = 3.14159 Private Sub Command1_Click() a = Val(txtInput.Text) If Option1.Value = True Then txtResult.Text = Format(Sin(a * pi / 180) - 0.0005, "0.000") ‘a*pi/180为固定公式 End If If Option2.Value = True Then txtResult.Text = Format(Cos(a * pi / 180) - 0.0005, "0.000") ‘-0.0005可以使值不会四舍五入 End If End Sub 七、(进阶)旋转表如下图布置界面,点击“开始”按钮线每一秒顺时针转动6° 答案: Dim lenth As Integer, q As Integer Const pi = 3.14159 Private Sub cmdStart_Click() tmrClock.Enabled = True End Sub Private Sub cmdStop_Click() tmrClock.Enabled = False End Sub Private Sub Form_Load() lenth = LinClock.Y2 - LinClock.Y1 q = 90 End Sub Private Sub tmrClock_Timer() q = q + 6 LinClock.Y1 = LinClock.Y2 - lenth * Sin(q * pi / 180) LinClock.X1 = LinClock.X2 - lenth * Cos(q * pi / 180) End Sub 八、找小于给定素数最大的三个素数设计如图所示界面,程序运行时, 在文本框中输入一个素数,然后单击“查找”命令按钮,找出小于给定素数的三个最大的素数,并显示在标签控件数组 lblResult 中,如图所示。 答案: Private Sub Command1_Click() Dim f As Boolean Dim n As Boolean Dim i As Long, j As Long, s As Long a = Val(Text1.Text) f = False n = False s = 0 For i = 2 To a - 1 If a Mod i = 0 Then MsgBox "请输入素数" f = False Exit For Else f = True End If Next i If f = True Then For i = a - 1 To 2 Step -1 n = False For j = 2 To i – 1 ‘注意是i-1,不是a-1 If i Mod j = 0 Then n = True Exit For Else n = False End If Next j If n = False Then Label3(s).Caption = i s = s + 1 End If If s = 3 Then Exit For End If Next i End If End Sub 九、求数列前n项设计如图所示界面,其功能是产生并显示一个数列的前n项。数列产生的规律是:数列的前2项是小于10的正整数,将此两数相乘,若乘积=10,则以乘积的十位数为数列的第3项,以乘积的个位数为数列的第4项。再用数列的最后2项相乘,用上述规则形成后面的项,直至产生了第n项。如图所示。 答案: Private Sub Command1_Click() Dim n() Dim chen As Integer Dim a As Integer, b As Integer, c As Integer Text4.Text = "" a = Text1.Text a = Val(a) b = Text2.Text b = Val(b) c = Text3.Text c = Val(c) ReDim n(1 To c) n(1) = a n(2) = b For i = 3 To c chen = n(i - 1) * n(i - 2) If chen >= 10 Then n(i) = chen \ 10 If i < c Then ’注意一定要小于c的时候,才能再输出个位的数加入数组当中,不然就 会超出数组范围 n(i + 1) = chen Mod 10 i = i + 1 End If Else n(i) = chen End If Next For i = 1 To c Text4.Text = Text4.Text & n(i) & " " Next End Sub 十、 红绿灯按照以下窗体搭建界面,使得shape1颜色红绿交替,两个水平滚动条控制时间1到10秒,点击开始按钮则shape1依照对应颜色滚动条时间,维持颜色的保留时间,只用到一个时钟控件。 答案: 时钟的Interval属性值设置为1000(也就是1秒) Dim green, red ‘定义两个变量,对应两个滚动条的值 Private Sub Command1_Click() Timer1.Enabled = True green = HScroll2.Value ‘对应 red = HScroll1.Value ‘对应 End Sub Private Sub Form_Load() Shape1.BackColor = vbRed ‘shape1的初始颜色为红色 End Sub Private Sub Timer1_Timer() If Shape1.BackColor = vbRed Then ‘当为红色时,所对应的变量red每秒-1。 red = red – 1 If red = 0 Then ‘如果变量red的值为0时,则shape1 Shape1.BackColor = vbGreen 的背景颜色变为绿色。(比如现在red End If 为3,每秒-1,说明3秒后为0,也就 说shape1的背景也是可以维持3秒红色。 而red的值又正好是水平滚动条的值。 ElseIf Shape1.BackColor = vbGreen Then ‘同理。 green = green - 1 If green = 0 Then Shape1.BackColor = vbRed End If End If If red = 0 Then ‘当red的值到0时,一定要复原,green一样 red = HScroll1.Value End If If green = 0 Then green = HScroll2.Value End If End Sub 总结:vb刷到这里,水平完全可以在技能考vb上那满分了,注意一些细节,后期会专门发一篇做vb题的细节。加油!对了,要多花时间在服务器配置上(Windows Server 2008 R2)上。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |