【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《【精品】常见vb程序设计代码》,欢迎阅读!
文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.
【关键字】精品
常见vb程序设计代码
作者 : 窦振山
1、猴子每天吃掉的桃子是所有桃子的一半多一个,到第七天发现只剩下一个了,问最开始有几个桃子?、、
Private Sub Command1_Click() Dim m(1 To 7) m(1) = 1
For i = 2 To 7
m(i) = (m(i - 1) + 1) * 2 Next
For i = 1 To 7
Print "第" & i & "天的桃数是"; m(i) Next End Sub
2、百元百鸡问题:公鸡三元一只,母鸡两元一只,小鸡一元两只,现用百元买一百只鸡 Private Sub Command1_Click() Dim x As Integer Dim y As Integer For x = 1 To 100 For y = 1 To 100
If 3 * x + 2 * y + (100 - x - y) / 2 = 100 Then Print "公鸡 "; x; "母鸡"; y; "小鸡"; 100 - x - y End If Next Next End Sub
1、综合运用一维数组、二维数组、动态数组和控件数组的有关知识编写程序,要求能输入学生的学号、姓名、性别、年龄等个人简况,输入的学生人数不限,并可按学号或姓名查询。 界面设计用到了三个command即“添加”、“查询”和“退出”,八个label和四个text用来输入学号、姓名、成绩和年龄,一个frame,一个picturebox图片框用来显示结果 其程序代码设计如下: Private Type stu id As Integer nam As String sco As Single age As Integer End Type
Dim students(100) As stu Dim s As Integer
Private Sub Command1_Click() Static i As Integer If Text1.Text = "" Then
文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.
MsgBox ("student's id can't be null!") Exit Sub Else
students(i).id = Val(Text1.Text) students(i).nam = Text2.Text
students(i).sco = Val(Text3.Text) students(i).age = Val(Text4.Text) End If
If i > 10 Then Exit Sub
Picture1.Print Tab(2); students(i).id; Tab(13); students(i).nam; Tab(24); students(i).sco; Tab(36); students(i).age Picture1.Print i = i + 1 End Sub
Private Sub Command2_Click()
s = Val(InputBox("enter the searched student's id")) For j = 0 To 10
If students(j).id = s Then Picture1.Cls
Picture1.Print Tab(2); students(j).id; Tab(13); students(j).nam; Tab(24); students(j).sco; Tab(36); students(j).age Exit For End If
If j = 100 Then MsgBox ("Student does not exit!") Next End Sub
Private Sub Command3_Click() End End Sub
产生50个60到100的随机数,统计其中被6整除余2的个数,并输出这些数 Private Sub Command1_Click() Dim x As Integer, n% Dim counter As Integer Randomize Timer For n = 1 To 50
x = Rnd() * Rnd() * 41 + 60 If x Mod 6 = 2 Then Print x
counter = counter + 1 End If Next n
Print counter End Sub
输入一个数,判断是不是素数:
文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.
Private Sub Command1_Click()
Var = Val(InputBox("请输入一个数")) For i = 2 To Var - 1 If Var Mod i = 0 Then MsgBox ("这不是素数") Exit For End If Next
If i = Var Then MsgBox ("这是素数") End Sub
此文档是由网络收集并进行重新排版整理.word可编辑版本!
本文来源:https://www.wddqxz.cn/b5fc99b7747f5acfa1c7aa00b52acfc788eb9fb5.html