《VB程序设计》课堂例题总结

更新时间:2024-04-19 11:05:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《VB程序设计》课堂例题总结

消息对话框的使用

Private Sub Command1_Click() If Not IsNumeric(Text1) Then

i% = MsgBox(\你输入的不是数字!\警告\ If i = 4 Then Text1.SetFocus Else End Else

MsgBox \你好\欢迎\End If End Sub

求圆的周长与面积

Private Sub Form_Click() Dim r!, s!, c!

Const pi = 3.14159 ?用户声明常量 r = InputBox(\请输入园的半径:\s = pi * r * r c = 2 * pi * r

MsgBox \园的周长为:\ 面积为:\End Sub

计算三角形的面积

Private Sub Command1_Click() Dim a%, b%, c%, s#, area# Dim str1$

a = Val(InputBox(\ b = Val(InputBox(\ c = Val(InputBox(\ s = (a + b + c) / 2

area = Sqr(s * (s - a) * (s - b) * (s - c)) str1 = \ MsgBox str1 Print str1 End Sub

字体的放大与缩小

随机放大1~3整数倍:Text1.FontSize = Text1.FontSize * Int(Rnd()*3+1) 缩小的比例为任意:Text1.FontSize = Text1.FontSize * Rnd()

连接与求和

Private Sub Command1_Click() Text3.Visible = True

1

Text3.Text = Text1.Text & Text2.Text '也可以用Text1.Text + Text2.Text Command1.Enabled = False End Sub

Private Sub Command2_Click()

Text3.Text = Val(Text1.Text) + Val(Text2.Text) End Sub

字符串的替换 方法一:(多个函数)

Private Sub Command1_Click()

i = InStr(Text1, Text2) '在Text1中查找出现Text2内容的位置 k = i + Len(Text2) ' 定位右子串的起始位置 Ls = Left(Text1, i - 1) ' 取左子串

Text4 = Ls + Text3 + Mid(Text1, k) ' 左子串连接替换为子串连接右子串实现替换 '也可以将上面两步合并为 Text4 = Left(Text1, i - 1) + Text3 + Mid(Text1, k) End Sub 方法二:(Replace函数) Private Sub Command2_Click()

Text4 = Replace(Text1, Text2, Text3) End Sub

身份证的查找

Private Sub Command1_Click()

Dim a As String, nl%, yue%, ri%, b%, xb$ a = Text1.Text

nl = 2010 - Val(Mid(a, 7, 4))

'nl = Year(Now()) - Val(Mid(a, 7, 4)) yue = Mid(a, 11, 2) ri = Mid(a, 13, 2) b = Val(Mid(a, 17, 1))

If b Mod 2 = 1 Then xb = \ Label3.Caption = nl

Label5.Caption = yue & \月\日\ Label7.Caption = \你是\End Sub

电话号码升位:将原来任意一个带区号的7位电话号码升到8位,在最前面加8。如020-1234567 → 020-81234567 区号及电话号码确定:

Left(s,4) & ”8” & Right(s,7) 区号及电话号码不确定:

Private Sub Command1_Click() Dim a As String

2

a = Text1.Text b = InStr(a, \

Label3.Caption = Left(a, b) & \ 'Label3.Caption = Left(a, b) & \End Sub

求一元二次方程的根

Private Sub Command1_Click() Dim a!, b!, c!, x1!, x2!, y!

a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text) y = b ^ 2 - 4 * a * c If y < 0 Then

MsgBox (\方程没有实数根\Else

x1 = (-b + Sqr(y)) / (2 *a) x2 = (-b - Sqr(y)) / (2 *a) Label2.Caption = x1 Label3.Caption = x2 End If End Sub

鸡兔同笼问题:鸡兔同笼,有h个头和f只脚,问有鸡兔各几只? 判断条件:

1.头和脚的数大于零 2.脚数为偶数

3.脚数是头数的2~4倍之间 设:有鸡c只,兔r只 h=c+r f=2c+4r c=(4h-f)/2 r=(f-2h)/2

Private Sub Command1_Click() Dim H As Integer, F As Integer Dim C As Integer, R As Integer H = Val(Text1.Text) F = Val(Text2.Text)

If F > 0 And H > 0 And F Mod 2 = 0 And F >= H * 2 And F <= H * 4 Then R = (F - 2 * H) / 2 C = H - R

Label3.Caption = \ Else

MsgBox \输入错\ End If End Sub

3

加密程序:将“明文”的大写字母随机向右移动1~3位,形成“密码”。

Private Sub Command1_Click() Dim m%,s$

Text1 = UCase(Text1) '变成大写字母

m = Int(Rnd * 3) + 1 '随机为生加密移动的位数 Label2.Caption = \向右移动的位数是:\s = Text1

If s >= \Chr(Asc(s) + m) <= \ '移动后不会超过字母\的字母 s = Chr(Asc(s) + m) '正常右移m位 Else

s = Chr(Asc(s) + m - 26) '移动后超过\的,要减去26 End If Label4 = s End Sub

Private Sub Form_Load() Randomize End Sub

加密程序:根据单选按钮的数字,将明文中每个字母改为其后第n个字母。

Private Sub Command1_Click() Dim m%, k% Text2 = \

m = IIf(Option1.Value = True, 1, IIf(Option2.Value = True, 2, 3)) For i = 1 To Len(Text1) s = Mid(Text1, i, 1)

If \ s = Chr(Asc(s) + m) Else

s = Chr(Asc(s) + m - 26) End If

Text2 = Text2 & s Next i End Sub

显示特殊图形

Private Sub Form_Click()

4

Print

For i = 1 To 5

Print Tab(i); String(6 - i, \★\▲\ Next i End Sub

登陆程序判断

Private Sub Command1_Click() If Text1.Text = \ MsgBox \祝贺你,成功登录!\ Else

MsgBox \对不起,口令错误,无法登录!\ Text1.Text = \ Text1.SetFocus End If End Sub

判断能否构成三角形,若能则判断是否等边、等腰、直角三角形

Private Sub Command1_Click() Dim a%, b%, c%

a =Val(InputBox(\ a =Val(InputBox(\ a =Val(InputBox(\

If a > 0 And b > 0 And c > 0 And a + b > c And _ b + c > a And a + c > b Then MsgBox \可以构成三角形\

If a = b And b = c Then

s = s & \,是等边三角形\

ElseIf a = b Or a = c Or b = c Then s = s & \,等腰三角形\

ElseIf a ^ 2 + b ^ 2 = c ^ 2 Or a ^ 2 + c ^ 2 = b ^ 2 Or c ^ 2 + b ^ 2 = a ^ 2 Then s = s & \直角三角形\ End If Else

MsgBox \不可以构成三角形\ End If End Sub

输入一个字符,判断该字符是字母字符、数字字符还是其它字符,并作相应的显示。 方法一:

Private Sub Command1_Click() Dim strc As String * 1

strc = InputBox(\请输入一个符号:\

5

If UCase(strc) >= \ Text1.Text = strc & \是字母\

ElseIf strc >= \ Text1.Text = strc & \是数字\Else

Text1.Text = strc & \是其它字符\End If End Sub 方法二:

Private Sub Command2_Click() Dim strc As String * 1

strc = InputBox(\请输入一个符号:\Select Case strc

Case \ Text1.Text = strc & \是字母\ Case \

Text1.Text = strc & \是数字\ Case Else

Text1.Text = strc & \是其它字符\End Select End Sub

模拟计算器 方法一:

Private Sub Command1_Click()

Dim sInput1 As Single, sInput2 As Single, oper As String * 1 Dim iError As Integer sInput1 = Val(Text1.Text) sInput2 = Val(Text3.Text) oper = Trim(Text2) If oper = \

Text4.Text = Str(sInput1 + sInput2) ElseIf oper = \

Text4.Text = Str(sInput1 - sInput2) ElseIf oper = \

Text4.Text = Str(sInput1 * sInput2) ElseIf oper = \

If sInput2 <> 0 Then

Text4.Text = Str(sInput1 / sInput2) Else

MsgBox (\分母为零,出错\ Text3.Text = \ Text3.SetFocus End If

6

Else

MsgBox (\运算符出错,再输入\ Text2.Text = \ Text2.SetFocus End If End Sub 方法二:

Private Sub Command2_Click()

Dim sInput1 As Single, sInput2 As Single Dim iError As Integer sInput1 = Val(Text1.Text) sInput2 = Val(Text3.Text) Select Case Trim(Text2) Case \

Text4.Text = Str(sInput1 + sInput2) Case \

Text4.Text = Str(sInput1 - sInput2) Case \

Text4.Text = Str(sInput1 * sInput2) Case \

If sInput2 <> 0 Then

Text4.Text = Str(sInput1 / sInput2) Else

MsgBox (\分母为零,出错\ Text3.Text = \ Text3.SetFocus End If Case Else

MsgBox (\运算符出错,再输入\ Text2.Text = \ Text2.SetFocus End Select End Sub

开始运行如左下图所示,单击“出题”按钮后计算机自动出题,窗口如右下图所示,此时由用户输入加法运算答案,然后单击“判定”按钮。如果用户运算正确则提示“运算正确”,否则显示“运算错误”。再次按下“出题”就会再出一道题。 Private Sub Form_Load() Randomize

Command1.Caption = \出题\End Sub

Private Sub Command1_Click()

If Command1.Caption = \出题\

Text1 = Int(Rnd * 101): Text2 = Int(Rnd * 101) Text3.SetFocus

7

Command1.Caption = \判定\ Else

If Val(Text1) + Val(Text2) = Val(Text3) Then MsgBox (\答对了,你真聪明!\ Command1.Caption = \出题\ Text1 = \ Else

MsgBox (\错了,努力啊!\ Text3 = \

Text3.SetFocus End If End If End Sub

Private Sub Command2_Click() End End Sub

修改密码登录程序。要求:建立一个登录窗口,要求输入密码。设定密码为“basic1234”(不区分大小写);若密码正确,显示“祝贺你,成功登录!”;若密码不正确,显示“对不起,密码错误,无法登录!”,并统计出错次数,当输入超过3次时,则退出系统。 Private Sub Command1_Click()

If LCase(Text2.Text) = \ MsgBox \祝贺你,成功登录!\ Else

MsgBox (\对不起,口令错误,无法登录!\ n = n + 1

Text2.Text = \ Text2.SetFocus

If n >= 3 Then Unload Me '也可以用End End If End Sub

Private Sub Text1_LostFocus()

If Len(Text1) <> 6 Or Not IsNumeric(Text1) Then MsgBox \帐号必须是6位数字!\ Text1 = \

Text1.SetFocus End If End Sub

分别输入年份和月份,根据输入数据显示该年该月有多少天? Private Sub Command1_Click() Dim y%,m%

y=InputBox(“输入年份:\m=InputBox(“输入月份:\

8

Select Case m

Case 1,3,5,7,8,10,12

MsgBox y & “年” & m & “月有31天!” Case 4,6,9,11

MsgBox y & “年” & m & “月有30天!” Case 2

If y mod 400=0 or y mod 4 _ =0 and y mod 100<> 0 Then MsgBox y & “年” & m & “月有29天!”

Else

MsgBox y & “年” & m & “月有28天!”

End If Case Else

Magbox “输入错误,月份应该是1-12之间!” End Select End Sub

求1+2+3+……+100的值 方法一:

Private Sub Command1_Click() Dim i%, sum% sum = 0

For i = 1 To 100 sum = sum + i Next i

Print \的和是:\End Sub 方法二:

Private Sub Form_Click() Dim i%, sum% sum = 0 i = 1

Do While i <= 100 sum = sum + i i = i + 1 Loop

Print \的和是:\End Sub

求任意数的阶乘

Private Sub Command2_Click() Dim i%, n%, sum# sum = 1

n = Val(InputBox(\请输入阶乘数“\For i = 1 To n

9

sum = sum * i Next

Print n; \的阶乘为\End Sub

输入若干个学生的成绩,求总分、平均分。输入时以-1作为结束标记。 Private Sub Command1_Click()

Dim n%, i%, sum%, max%, mark%, aver! mark = Val(InputBox(\请输入成绩\Do While mark <> -1 Print mark

If max < mark Then max = mark sum = sum + mark n = n + 1

mark = Val(InputBox(\请输入成绩\Loop

aver = sum / n

Print \总分为:\Print \平均为:\Print \最高分为:\End Sub

找出1~100之间所有能被3整除并且个位数字为2的数。 Private Sub Command1_Click()

Dim i%

For i = 1 To 100

If i Mod 3 = 0 And i Mod 10 = 2 Then Print i End If Next End Sub

输入一字符串,统计字母 “a”(包括大小写)的个数。 Private Sub Command1_Click() Dim i%, s$, ch$, n%

s = InputBox(\请输入一串字符串:\Print \字符串为:\n = 0

For i = 1 To Len(s) ch = Mid(s, i, 1)

If LCase(ch) = \Next

Print \字符a的个数为:\

10

End Sub

将输入的字符串首尾颠倒后输出。 方法一:

Private Sub Command1_Click() Dim i%, s1$, ch$, s2$

s1 = InputBox(\请输入一串字符串:\Print \原始字符串为:\For i = 1 To Len(s1) ch = Mid(s1, i, 1) s2 = ch & s2 Next

Print \倒序后字符串为:\End Sub 方法二:

Private Sub Command2_Click() Dim i%, s1$, ch$, s2$

s1 = InputBox(\请输入一串字符串:\Print \原始字符串为:\For i = Len(s1) To 1 Step -1 ch = Mid(s1, i, 1) s2 = s2 & ch Next

Print \倒序后字符串为:\End Sub 方法三:

Private Sub Command3_Click() Dim i%, n%, s$, ch$

s = InputBox(\请输入一串字符串:\Print \原始字符串为:\n = Len(s)

For i = 1 To n / 2 ch = Mid(s, i, 1)

Mid(s, i, 1) = Mid(s, n - i + 1, 1) Mid(s, n - i + 1, 1) = ch Next

Print \倒序后字符串为:\End Sub

统计其中分别有多少个大写字母、小写字母、数字和其他字符并输出结果 Private Sub Command1_Click() Dim i%, s$, ch$, n1%, n2%, n3%

s = InputBox(\请输入一串字符串:\Print \字符串为:\

11

n1 = 0: n2 = 0: n3 = 0 For i = 1 To Len(s) ch = Mid(s, i, 1)

If LCase(ch) >= \ n1 = n1 + 1

ElseIf ch >= \ n2 = n2 + 1 Else

n3 = n3 + 1 End If Next

Print \字母的个数为:\Print \数字的个数为:\Print \其他字符的个数为:\End Sub

输入10个同学考试的等级,如果A计5分,B计4分,以此类推,求这10个同学的平均成绩。

Private Sub Form_Click() Dim i%, sum%, mark$, aver! For i = 1 To 10

mark = InputBox(\请输入成绩\ Print mark,

If i Mod 5 = 0 Then Print Select Case UCase(mark)

Case \ sum = sum + 5 Case \ sum = sum + 4 Case \ sum = sum + 3 Case \ sum = sum + 2 Case \ sum = sum + 1 Case Else: sum = sum + 0 End Select Next

aver = sum / 10

Print \平均为:\End Sub

输出斐波那契级数1、1、2、3、5、8、13……的前30项。此级数项的规律是:前两项的值各为1,从第3项起,每一项是前2项的和。(要求一行输出6项) Private Sub Command1_Click() Dim a#, b#, c#, i% a = 1 '第一项为1 b = 1 '第二项为1

12

Print a; b; '打印前两项

For i = 3 To 30 '求剩余的28项

c = a + b '每一项都是前两项之和 Print c; '打印输出

If i Mod 6 = 0 Then Print '一行打印6个即换行 a = b '新的第一项 b = c '新的第二项 Next End Sub

编写程序输出2000年至2500年间的所有闰年,要求每行输出8个 Private Sub Command1_Click() Dim y%, n%

For y = 2000 To 2500

If y Mod 400 = 0 Or y Mod 4 = 0 And y Mod 100 <> 0 Then Print y

If y Mod 8 = 0 Then Print End If Next y End Sub

回文字串:香莲碧水动风凉,水动风凉夏日长。长日夏凉风动水,凉风动水碧莲香。 Private Sub Command1_Click() Dim s$, n%, i%

s = InputBox(\请输入一个整数:\n = Len(s)

For i = 1 To n / 2

If Mid(s, i, 1) <> Mid(s, n - i + 1, 1) Then Print s; \不是回文数!\ Exit For End If Next

If i > n / 2 Then Print s; \是回文数!\End Sub

输入一个正整数,判断该数是否为素数。(素数是指一个数只能被1和其本身整除。) Private Sub Command1_Click() Dim i%, n%, s$

n = Val(InputBox(\请输入一个正整数:\s = n & \是素数\For i = 2 To n - 1

If n Mod i = 0 Then s = n & \不是素数\ Exit For

13

End If Next Print s End Sub

随机产生10个100~200之间的数,找出其中的最大值。 Private Sub Command1_Click() Dim i%, a%, max% Cls

Randomize

a = Int(Rnd * 101) + 100 '先产生一个数,作为最大值的初值 max = a Print a

For i = 2 To 10 '紧跟着产生剩下的9个数 a = Int(Rnd * 101) + 100 Print a

If a > max Then max = a

'拿新产生的数来与最大值作比较,若比最大值大,则取代其,作为新的最大值 Next

Print \最大值为\End Sub

计算1!+2!+3!+...+10! 的值 Dim sum#, i%, j%, n# sum = 0: n = 1 For i = 1 To 10 n = 1

For j = 1 To i n = n * j Next j

sum = sum + n Next i

Print \!+2!+3!+...+10!=\

显示九九乘法表

Private Sub Form_Click() Dim i%, j% Cls

Print \ |\For i = 1 To 9 Print \Next Print

Print \

14

For i = 1 To 9 Print i; \ For j = 1 To i Print i * j; Next j Print Next i End Sub

打印如下图图案

Private Sub Form_Click() Dim i%, j% Cls

For i = 1 To 9

Print Space(11 - i); For j = 1 To 2 * i - 1 Print \ Next j Print Next i

For i = 8 To 1 Step -1

Print Space(11 - i); String(2 * i - 1, \Next End Sub

输出100以内的素数 Private Sub Form_Click() Dim i%, n%,Tag as Boolean For n= 1 to 100 Tag= True For i = 2 To n - 1

If n Mod i = 0 Then Tag=False Exit For End If Next i

15

If Tag=True then Print n Next n End sub

穷举法:百元买百鸡问题。假定小鸡每只5角,公鸡每只2元,母鸡每只3元。现在有100元钱要求买100只鸡,编程列出所有可能的购鸡方案。 方法一:

Private Sub Command1_Click() Dim x%, y%, z% For x= 0 to 100 For y= 0 to 100

For z= 0 to 100 step 2

If x+y+z=100 and 3*x+2*y+z*0.5=100 Then Print x,y,z End If Next z,y,x End Sub 方法二:

Private Sub Command1_Click() Dim x%, y%, z% For i = 0 To 20 For j = 0 To 33

For k = 0 To 100 Step 3

If i + j + k = 100 And i * 5 + j * 3 + k / 3 = 100 Then Print \公鸡:\母鸡:\小鸡:\ End If Next k Next j Next i End Sub

编写程序,显示所有的水仙花数(所谓水仙花数是指一个3位数,期各位数字立方和等于该数字本身,如153=1^3+5^3+3^3)。

方法一(用单循环完成,分别把三位数中的每个数字取出来) Private Sub Command1_Click() Dim i%, g%, s%, b% For i = 100 To 999 b = i \\ 100 g = i Mod 10

s = (i \\ 10) Mod 10

If b ^ 3 + g ^ 3 + s ^ 3 = i Then Print i Next End Sub

方法二(用三重循环完成,由三个数字组成三位数来判断)

16

Private Sub Form_Click() Dim i%, j%, k%, s% For i = 1 To 9 For j = 0 To 9 For k = 0 To 9

If i ^ 3 + j ^ 3 + k ^ 3 = i * 100 + j * 10 + k Then Print i * 100 + j * 10 + k Next k Next j Next i End Sub

输入一个班100个学生的成绩,要求:1)求平均分2)统计高于平均分的人数 Private Sub Form_Click()

Dim a%(1 To 100),i%, SUM%, aver!,n% For i = 1 To 100

a(i) = InputBox(\ Print a(i)

SUM = SUM + a(i) Next i

aver = SUM / 100 For i = 1 To 100

If a(i) > aver Then n = n + 1 Next i Print n End Sub

斐波那契数列,即:1,1,2,3,5,8,13……由此可知:a1=a2=1 ,an=an-1+an-2 (用数组求) Private Sub Form_Click() Dim i%,f&(30) f(1)=1:f(2)=1 Print f(1);f(2); For i=3 to 30 f(i)=f(i-2)+f(i-1) Print f(i);

If i mod 5 = 0 Then Print End If Next i End Sub

用计算机模拟掷色子游戏。一个色子有六个点数,编写程序统计掷N次(N尽量大,例如100000次)后各点子出现的次数。 方法一:

Private Sub Command1_Click() Dim c%, i&

17

Dim n(1 To 6) As Single '定义一个数组记算所掷每一个色子的次数 For i = 1 To 100000

c = Int(Rnd * 6 + 1) '我们所扔的色子的点数实际是一个1~6的随机数 n(c) = n(c) + 1 '对于所要记录的这个色子对应的个数加1 Next i

For i = 1 To 6

Print \Next End Sub 方法二:

Private Sub Form_Click()

Dim dian%, i&, n1&, n2&, n3&, n4&, n5&, n6& For i = 1 To 100000 dian = Int(Rnd * 6) + 1 If dian = 1 Then n1 = n1 + 1

ElseIf dian = 2 Then n2 = n2 + 1

ElseIf dian = 3 Then n3 = n3 + 1

ElseIf dian = 4 Then n4 = n4 + 1

ElseIf dian = 5 Then n5 = n5 + 1 Else

n6 = n6 + 1 End If Next

Print n1:Print n2:Print n3:Print n4:Print n5:Print n6 End Sub

输入一串字符,分别统计各字母出现的次数,不区分字母大小写。 Private Sub Form_Click() Dim a%(65 To 90), s$

s = InputBox(\输入一串字符串:\Print \字符串为\For i = 1 To Len(s)

n = Asc(UCase(Mid(s, i, 1))) a(n) = a(n) + 1 Next

For i = 65 To 90

If a(i) <> 0 Then Print Chr(i); \的个数为:\Next End Sub

18

将一个数组中的元素按逆序存放 Private Sub Form_Click() Dim i%, a%(10), t% Randomize For i = 1 To 10

a(i) = Int(Rnd * 101) + 100 Print a(i);

If i Mod 5 = 0 Then Print Next

For i = 1 To 5

t = a(i): a(i) = a(10 - i + 1): a(10 - i + 1) = t Next

Print \逆序输出结果为:\For i = 1 To 10 Print a(i);

If i Mod 5 = 0 Then Print Next End Sub

10个元素的数组求最大值。 Private Sub Form_Click() Dim i%, a%(10), max%

a(1) = Int(Rnd * 101) + 100 '先产生一个数,作为最大值的初值 max = a(1) Print a(1)

For i = 2 To 10 '紧跟着产生剩下的9个数 a(i) = Int(Rnd * 101) + 100 Print a(i)

If a(i) > max Then max = a(i)

'拿新产生的数来与最大值作比较,若比最大值大,则取代其,作为新的最大值 Next

Print \最大值为\End Sub

打印杨辉三角

Option Base 1

Private Sub Form_Click() Dim a%(7, 7)

19

For i = 1 To 7 For j = 1 To i

If j = 1 Or i = j Then a(i, j) = 1 Else

a(i, j) = a(i - 1, j) + a(i - 1, j - 1) End If

Print a(i, j); Next Print Next End Sub

通过Inputbox输入5个同学的姓名和身高,显示全部同学的姓名和身高及最高的同学的姓名和身高。 Option Base 1

Private Sub Command1_Click() Dim a(5, 2), i%

a(1, 1) = Val(InputBox(\请输入第一个身高:\ a(1, 2) = InputBox(\请输入第一个姓名:\ Max = a(1, 1) maxi = 1 For i = 2 To 5

a(i, 1) = Val(InputBox(\请输入第\个身高:\ a(i, 2) = InputBox(\请输入第\个姓名:\ If a(i, 1) > Max Then Max = a(i, 1) maxi = i End If Next i

For i = 1 To 5

Print a(i, 1), a(i, 2) Next i

Print \最高的是:\身高\End Sub

数组求最大值的下标 Private Sub Form_Click() Dim i%, a%(10), maxi% a(1) = Int(Rnd * 101) + 100 maxi = 1 '记录最大值的下标 Print a(1)

For i = 2 To 10 '紧跟着产生剩下的9个数 a(i) = Int(Rnd * 101) + 100

20

Print a(i)

If a(i) > a(maxi) Then maxi = i '记录最大值的下标

'拿新产生的数来与最大值作比较,若比最大值大,则取代其,作为新的最大值 Next

Print \最大值为第\个元素,值为\End Sub

随机产生20个学生的成绩,统计各分数段人数。 Option Explicit

Private Sub Command1_Click() Dim a(1 To 20) As Integer Dim s(5 To 9) As Integer Dim i%, j% Cls

For i = 1 To 20

a(i) = Int(Rnd * 101)

Print \ If i Mod 5 = 0 Then Print j = a(i) \\ 10 Select Case j Case 0 To 5

s(5) = s(5) + 1 Case 6 To 8

s(j) = s(j) + 1 Case Else

s(9) = s(9) + 1 End Select Next i Print

For i = 5 To 9

Print \Next End Sub

随机产生10个0~50之间不重复的数字 Option Base 1

Private Sub Form_Load() Randomize End Sub

Private Sub Command1_Click() Dim a%(10), y% Cls

a(1) = Int(Rnd * 51) '产生第一个数 i = 2

21

Do While i <= 10 '循环产生后面9个数

y = Int(Rnd * 51) '随机产生一个但不确定是否重复,所以不直接放进数组 For j = 1 To i - 1 '与之前产生的i-1进行比较,检查是否重复 If y = a(j) Then Exit For '发现重复则退出循环 Next j

If j > i - 1 Then

a(j) = y '不重复则放入数组 i = i + 1 '为产生下一个数做准备 End If Loop

For i = 1 To 10 Print a(i) Next i End Sub

输入学生的人数及科目数,再输入各位学生的各科课程的考试成绩,编写程序可以计算各个学生的平均成绩及每门课程的平均成绩 Option Base 1

Private Sub Command1_Click()

Dim a%(), n%, m%, i%, j%, sum%, aver! n = Val(Text1) m = Val(Text2) ReDim a(n, m) For i = 1 To n sum = 0

For j = 1 To m

a(i, j) = InputBox(\请输入第\个学生的第\门课程成绩:\ sum = sum + a(i, j) Next j

aver = sum / m

Label3.Caption = Label3.Caption & \第\个学生的平均成绩为:\Next i

For j = 1 To m sum = 0

For i = 1 To n

sum = sum + a(i, j) Next

aver = sum / n

Label3.Caption = Label3.Caption & \第\门课程的平均成绩为:\Next End Sub

裴波那切数列(动态数组求) Private Sub Command2_Click()

22

Dim a&() ReDim a(2)

a(1) = 1: a(2) = 1 Cls

Print a(1); a(2); i = 3

Do While True

ReDim Preserve a(i) a(i) = a(i - 2) + a(i - 1) Print a(i);

If i Mod 3 = 0 Then Print If a(i) > 30000 Then Exit Do i = i + 1 Loop End Sub

已知数组,输入一个数,查找该数是数组中第几个元素的值。如果该数不在数组中,则显示该数不在数组中 Option Base 1 Dim a()

Private Sub Command1_Click()

a = Array(2, 8, 16, 72, 36, 9, 3, 10, 25) For i = LBound(a) To UBound(a) Text1 = Text1 & Str(a(i)) Next i End Sub

Private Sub Command2_Click() Dim i%, y%

y = InputBox(\请输入要查找的数:\For i = LBound(a) To UBound(a) If a(i) = y Then Exit For Next i

If i <= UBound(a) Then

Label1.Caption = \找到了,是数组的第\个元素。\Else

Label1.Caption = \数组中没有这个数。\End If End Sub

已知有序数组,输入一任意数,将其插入数组中,使插入后的数组仍旧有序。 Private Sub Command1_Click() Dim a(),i%,k%,x%

a = Array(1,4,7,10,13,16,19,22,25)

x = Val(InputBox(\请输入要插入的数据:\

23

For k = LBound(a) To UBound(a) If x

ReDim Preserve a( UBound(a) + 1 ) For i = UBound(a)-1 To i Step -1 a(i + 1) = a(i) Next i a(k) = x

For i = LBound(a) To UBound(a) Print a(i); Next i End Sub

数组中n个数进行排序(选择排序法与冒泡排序法) Dim a%()

Private Sub Form_Load() Randomize End Sub

Private Sub Command1_Click() '产生数组 Print \原数组为:\

n = InputBox(\数组元素的个数:\ ReDim a(n)

For i = 1 To UBound(a) a(i) = Int(Rnd * 200 + 1) Print a(i); Next i Print End Sub

Private Sub Command2_Click() '选择法排序 For i = 1 To UBound(a) - 1 imin = i

For j = i + 1 To UBound(a)

If a(j) < a(imin) Then imin= j Next j t = a(i)

a(i) = a(imin) a(imin) = t Next i End Sub

Private Sub Command3_Click() '冒泡法排序

For i = 1 To UBound(a) - 1 ' 进行n-1轮比较

For j = UBound(a) To i + 1 Step -1 ' 从n~i个元素进行两两比较

24

If a(j) < a(j - 1) Then ' 若次序不对,则马上进行交换位置 t = a(j)

a(j) = a(j - 1) a(j - 1) = t End If

Next j ' 出了内循环,一轮排序结束,最小数已冒到最上面 Next i End Sub

Private Sub Command4_Click() '显示结果 Print \排序后数组为:\ For i = 1 To UBound(a) Print a(i); Next i Print End Sub

创建如图所示拨号器

Private Sub Command1_Click(Index As Integer) Text1.Text = Text1.Text & Index End Sub

Private Sub command2_Click() If Len(Text1.Text) > 0 Then

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) End If End Sub

Private Sub command3_Click() Text1.Text = \End Sub

用复选框设置合计的项目。

25

Private Sub Command1_Click()

Dim yw%, sx%, yy%, wl%, hx%, sum% yw = Val(Text1.Text) sx = Val(Text2.Text) yy = Val(Text3.Text) wl = Val(Text4.Text) hx = Val(Text5.Text)

If Check1.Value = 1 Then sum = sum + yw If Check2.Value = 1 Then sum = sum + sx If Check3.Value = 1 Then sum = sum + yy If Check4.Value = 1 Then sum = sum + wl If Check5.Value = 1 Then sum = sum + hx Text6.Text = sum End Sub

用单选按钮设置计算的项目。

Private Sub Command1_Click() Dim i%, a%, b%, sum%

If Option1.Value = True Then For i = 100 To 200 Step 2 sum = sum + i Next Else

For i = 201 To 400 Step 2 sum = sum + i Next End If

Text1.Text = sum End Sub

用单选钮和复选框设置文本框的文字格式

26

Private Sub Check1_Click()

Text1.FontBold = Not Text1.FontBold End Sub

Private Sub Check2_Click() If Check2.Value = 1 Then Text1.FontItalic = True Else

Text1.FontItalic = False End If End Sub

Private Sub Check3_Click()

Text1.FontUnderline = Not Text1.FontUnderline End Sub

Private Sub Combo1_Click()

Text1.FontSize = Combo1.Text End Sub

Private Sub Command1_Click() Unload Me End Sub

Private Sub Form_Load()

Text1.Text = \白日依山尽,\黄河入海流,\欲穷千里目,\Chr(13) + Chr(10) & \更上一层楼。\ Text1.FontSize = Combo1.Text For i = 5 To 40

Combo1.AddItem 2 * i Next End Sub

Private Sub List1_Click()

Text1.FontName = List1.Text End Sub

Private Sub Option4_Click() Text1.ForeColor = vbBlack End Sub

Private Sub Option5_Click() Text1.ForeColor = vbRed End Sub

Private Sub Option6_Click() Text1.ForeColor = vbBlue

27

End Sub

窗体上有一文本框,一列表框、一标签和三个命令按钮,标题分别为“添加”、“删除”和“清除”。单击“添加”按钮,则把文本框中的内容添加到列表框中;单击“删除”按钮,则删除列表框中选中的项目。单击“清除”按钮,则清除列表框中的所有选项。当列表框中的数目发生变化的时候,用标签显示列表框中的项目数。

Private Sub Form_Load()

List1.AddItem \计算机文化基础\ List1.AddItem \程序设计教程\ List1.AddItem \操作系统\ List1.AddItem \多媒体技术\ List1.AddItem \网络技术基础\

Label1.Caption = \列表框项目数:\End Sub

Private Sub List1_Click() Text1.Text = List1.Text End Sub

Private Sub Command1_Click() List1.AddItem Text1.Text Text1 = \

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command2_Click() List1.RemoveItem List1.ListIndex

'以下的这段代码适合于多项选择后删除 'For i = List1.ListCount - 1 To 0 Step -1 ' If List1.Selected(i) = True Then ' List1.RemoveItem i ' End If 'Next

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command3_Click() List1.Clear

Label1.Caption = \列表框项目数:\End Sub

Private Sub Command4_Click()

28

For i = 0 To List1.ListCount - 1 If List1.List(i) = Text1.Text Then

MsgBox \列表框中已有\ Text1.Text = \ Text1.SetFocus Exit For End If Next

If i >= List1.ListCount - 1 Then List1.AddItem Text1.Text End If

Label1.Caption = \列表框项目数:\End Sub

输出100以内的素数

Dim i%, n%,Tag as Boolean For n= 2 to 100 Tag= True

For i = 2 To n–1 ?也可以是 2 to Sqr(n) If n Mod i = 0 Then Tag=False Exit For End If Next i

If Tag=True then Print n Next n

列表框或组合框的操作 提取选中的内容 Text1 = List1.Text 替换选中的内容

List1.List(List1.ListIndex) = Text1 操作列表框中的所有内容 For i = 0 To List1.ListCount - 1

要进行的操作,可用List1.List(i)表示各个项目 Next

操作列表框中选中的内容 For i=0 To List1.ListCount–1

(或者是For i=List1.ListCount-1 To 0 Step -1) If List1.Selected(i) = True Then 要进行的操作 Next

程序运行后,将把1~100之间能够被7整除的数添加到列表框中。并实现按钮的功能。

29

Private Sub Command1_Click() Dim i%, sum% sum = 0

For i = 1 To List1.ListCount sum = sum + List1.List(i - 1) Next

Text1.Text = sum End Sub

Private Sub Command2_Click() Dim i%, sum% sum = 0

For i = 0 To List1.ListCount - 1 If List1.Selected(i) = True Then sum = sum + List1.List(i) End If Next

Text1.Text = sum End Sub

Private Sub Command3_Click() Dim i%

For i = List1.ListCount - 1 To 0 Step -1 If List1.Selected(i) = True Then List1.RemoveItem i End If Next End Sub

Private Sub Form_Load() Dim i%

For i = 1 To 100

If i Mod 7 = 0 Then List1.AddItem i End If Next End Sub

点菜配餐

30

Private Sub Command1_Click() Dim i%

For i = 0 To List1.ListCount - 1

If List1.List(i) = Combo1.Text Then

MsgBox \你已经点过了\ Exit Sub End If Next

List1.AddItem Combo1.Text End Sub

Private Sub Command2_Click() Dim i%, t As Boolean t = True

For i = 0 To List1.ListCount - 1

If List1.List(i) = Combo2.Text Then t = False

MsgBox \你已经点过了\ Exit For End If Next

If t = True Then List1.AddItem Combo2.Text End Sub

Private Sub Command3_Click() Dim i%

For i = List1.ListCount - 1 To 0 Step -1

If List1.Selected(i) = True Then List1.RemoveItem i Next End Sub

Private Sub Command4_Click() List1.Clear End Sub

Private Sub Form_Load()

Combo1.AddItem \宫爆鸡丁\Combo1.AddItem \青菜蘑菇\Combo1.AddItem \清蒸鲈鱼\Combo1.AddItem \清蒸排骨\Combo1.AddItem \红烧牛肉\

31

Combo1.AddItem \清蒸螃蟹\Combo1.AddItem \鱼香肉丝\Combo1.AddItem \芋头扣肉\Combo2.AddItem \奶茶\Combo2.AddItem \牛奶\Combo2.AddItem \豆浆\Combo2.AddItem \可乐\Combo2.AddItem \雪碧\Combo2.AddItem \芬达\Combo2.AddItem \啤酒\Combo2.AddItem \红酒\End Sub

调色板

Rem 注意VB中RGB函数表示颜色的用法,

Rem 以及滚动条的Change事件与Scroll事件的不同之处。

Dim R%, G%, B% '注意变量说明语句的位置,因为在几个过程中均用到这三个变量, '所以要在“通用”“声明”中说明是全局变量 Private Sub Form_Load()

Label5.Caption = \End Sub

Private Sub HScroll1_Scroll() R = HScroll1.Value Label2.Caption = R

Label1.BackColor = RGB(R, G, B)

Label5.Caption = \End Sub

Private Sub HScroll2_Change() G = HScroll2.Value Label3.Caption = G

Label1.BackColor = RGB(R, G, B)

Label5.Caption = \End Sub

Private Sub HScroll3_Change() B = HScroll3.Value Label4.Caption = B

32

Label1.BackColor = RGB(R, G, B)

Label5.Caption = \End Sub

在窗体的Label中动态显示系统时间。(即每隔1秒显示一次系统时间)

(Interval = 1000每隔1秒触发1次Timer事件)

Private Sub Form_Load() Label1.Caption = Time() End Sub

Private Sub Timer1_Timer() Label1.Caption = Time() End Sub

倒计时

Dim t As Integer, mm As Integer, ss As Integer, max!, min! Private Sub Command1_Click()

t = InputBox(\请以秒为单位输入倒计时时间\ mm = t \\ 60 ss = t Mod 60

Label1.Caption = mm & \分\秒\ ProgressBar1.max = t ProgressBar1.min = 0 ProgressBar1.Value = t End Sub

Private Sub Command2_Click() Timer1.Enabled = True End Sub

Private Sub Form_Load() Timer1.Enabled = False End Sub

Privite Sub Timer1_Timer() t = t - 1 mm = t \\ 60 ss = t Mod 60

ProgressBar1.Value = t

33

Label1.Caption = mm & \分\秒\ If t = 0 Then

Timer1.Enabled = False Label1.Caption = \时间到!\ End If End Sub

移动控件位置的两种代码

对象.Left = x; 对象.Top = y Label1.Left=Label1.Left + 50 Label1.Top=Label1.Top + 50

或用Move方法:对象.Move x, y Label1.Move Label1.Left - 50

Label1.Move Label1.Left, Label1.Top + 50

窗体有一个矩形和一个圆,还有一水平滚动条和一垂直滚动条。程序运行时,移动某个滚动条的滑块,可使圆作相应方向的移动。滚动条刻度值的范围是圆可以在矩形中移动的范围。

Private Sub Form_Load()

Vscroll1.Max = Shape1.Top + Shape1.Height - Shape2.Height Vscroll1.Min = Shape1.Top Hscroll1.Min = Shape1.Left

Hscroll1.Max = Shape1.Left + Shape1.Width - Shape2.Width End Sub

Private Sub Hscroll1_Change() Shape2.Left = Hscroll1.Value End Sub

Private Sub Vscroll1_Change() Shape2.Top = Vscroll1.Value End Sub

小球运动。小球在窗体上任意运动,超出任意边界都能自动弹回来。 Dim h%, w%

Private Sub Form_Load() h = 10 w = 15 End Sub

Private Sub Timer1_Timer()

34

Shape1.Left = Shape1.Left + w Shape1.Top = Shape1.Top + h

If Shape1.Left <= 0 Or Shape1.Left + Shape1.Width >= Width Then w = -w End If

If Shape1.Top <= 0 Or Shape1.Top + Shape1.Height >= Height Then h = -h End If End Sub

根据文本框中所输入的数据,滚动条向对应方向移动相应刻度,要求:超过最大刻度或最小刻度则给出相应提示信息

Private Sub Command1_Click()

If HScroll1.Value + Text1.Text > HScroll1.Max Then MsgBox \输入数据太大\

ElseIf HScroll1.Value + Text1.Text < HScroll1.Min Then MsgBox \输入数据太小\Else

HScroll1.Value = HScroll1.Value + Text1.Text End If End Sub

m求组合数 n

Public Function JC#(k%) Dim i% JC = 1 For i = 1 to k JC = JC * i Next

End Function

Private Sub Command1_Click() Dim n%, m%, i%, s1#, s2#, s3# n = Val(InputBox(\输入N\ m = Val(InputBox(\输入M\S1=JC(n) S2=JC(m) S3=JC(n-m)

Print n; \选\有\种组合!\

n!c?m!(n?m)! 35

已知多边形各条边及对角线的长度,要计算多边形的面积。 Private Sub Command1_Click()

Dim a!, b!, c!, d!, e!, f!, g!, s1!, s2!, s3! a = InputBox(输入边长a) b = InputBox(输入边长b) c = InputBox(输入边长c) d = InputBox(输入边长d) e = InputBox(输入边长e) f = InputBox(输入边长f) g = InputBox(输入边长g) s1 = area(a, b, c) s2 = area(c, d, e) s3 = area(e, f, g) Print s1 + s2 + s3 End Sub

Public Function area(x!, y!, z!) As Single Dim c!

c = 1 / 2 * (x + y + z)

area = Sqr(c * (c - x) * (c - y) * (c - z)) End Function

编写判断素数的函数过程Prime(n),函数的返回值类型为布尔型。(重点) Function Prime(n%) As Boolean tag = True

For i = 2 To n - 1

If n Mod i = 0 Then tag = False Exit For End If Next Prime = tag End Function

Private Sub Text1_KeyPress(KeyAscii As Integer) Dim tag As Boolean,i%,n% If KeyAscii = 13 Then n = Val(Text1.Text)

If Prime(n) = True Then P1.Print n;”是素数” Else

P1.Print n;”不是素数” End If End If End Sub

36

判断素数,并求范围内的素数之和。(重点)

Dim sum%

Private Sub Command1_Click() Dim n1%, n2%, i% sum = 0

If Option1.Value Then a = 100: b = 200 Else

a = 200: b = 400 End If

For i = a To b

If prime(i) Then sum = sum + i Next i

Text1 = sum End Sub

Function prime(n%) As Boolean prime = True

For i = 2 To Sqr(n) If n Mod i = 0 Then prime = False Exit For End If Next i

End Function

编写求一维数组的最小值的子过程或函数。 Option Base 1

Dim arr(10), b(), s()

Private Sub Command1_Click() Dim i%

For i = 1 To 10

arr(i) = Int(Rnd * 101) + 100 Print arr(i); Next i Print

b = Array(18, 12, 65, 42, 85, 96, 68, 75) s = Array(\

Print \

37

Print \ Print End Sub

Function FunMin(a()) As Variant ?函数过程 Dim i%, min

min = a(LBound(a))

For i = LBound(a) + 1 To UBound(a) If a(i) < min Then min = a(i) Next

FunMin = min End Function

Sub ProcMin(a(), min) ?子过程 Dim i%

min = a(LBound(a))

For i = LBound(a) + 1 To UBound(a) If min > a(i) Then min = a(i) Next i End Sub

Private Sub Command2_Click() Print \调用过程求最小:\ ProcMin arr, mm

Print \数组的最小值为\ ProcMin b, mm

Print \数组的最小值为\ ProcMin s, mm

Print \数组的最小值为\ Print End Sub

Private Sub Command3_Click() Print \调用函数求最小:\

Print \数组的最小值为\ Print \数组的最小值为\ Print \数组的最小值为\ Print End Sub

把文本框Mytxt的内容,写入文件Myfile.dat中 方法1:把整个文本框的内容一次性地写入文件。 Open App.Path + “\\Myfile.dat\ For Output As #1 Print #1, Mytxt.Text Close #1

方法2:把整个文本框的内容一个字符一个字符地写入文件。 Open “.\\Myfile.dat\For i=1 To Len(Mytxt.Text)

38

Print #1,Mid(Mytxt.Text,i,1); Next i Close #1

随机产生20个[50,100]的随机整数,存入数组中,并把数组的各元素值(按每行5个元素输出)写入文件MyList.dat中 Private Sub Command1_Click() Dim a%(20), i%

Open “D:\\Mylist.dat\ ?打开文件 For i = 1 To 20

a(i) = Int(Rnd * 51) + 50

Print #1, a(i), ?写文件

If i Mod 5 = 0 Then Print #1, ?在文本文件中换行 Next

Close #1 ?关闭文件 End sub

将文本文件MYFILE.TXT的内容读到文本框Text1中。 Text1.Text = \ Open “D:\\ myfile.txt\ Do While Not EOF(1) Line Input #1, s Text1.Text = Text1.Text & s & vbCrLf Loop Close #1

将文件MyList.dat的数据读出来存放到数组中,并在列表框中显示 Option Base 1

Private Sub Command2_Click() Dim a(), i%

Open \ i = 0

Do While Not EOF(1) i = i + 1

ReDim Preserve a(i) Input #1, a(i)

List1.AddItem a(i) Loop Close #1 End Sub

三种读文件方式的区别

Private Sub Command1_Click()

Open \

39

Do While Not EOF(1) Line Input #1, s List1.AddItem s Loop Close End Sub

Private Sub Command2_Click()

Open \Do While Not EOF(1) s = Input(1, 1) List2.AddItem s Loop Close End Sub

Private Sub Command3_Click()

Open \Do While Not EOF(1) Input #1, s List3.AddItem s Loop Close End Sub

过程(自定义函数与子过程)与文件读写结合 Option Base 1 Dim a%(4, 4)

Dim max%, line3%, num% Private Sub Form_Load() Randomize Label2 = \End Sub

Private Sub Command1_Click(Index As Integer) Dim i%, j%

Select Case Index Case 0

Open App.Path & \ '打开数据文件,准备写入 For i = 1 To 16

num = Int(Rnd * 30 + 1)

Print #2, num; '将数据写入磁盘文件

If i Mod 4 = 0 Then Print #2, '在数据文件中换行 Next i Close #2

MsgBox \已写入文件。\ Case 1

40

Open App.Path & \ '打开数据文件,准备读出 For i = 1 To 4 For j = 1 To 4

Input #2, a(i, j) '读磁盘文件中的一个数据,并赋值给数组元素

s$ = IIf(Len(CStr(a(i, j))) = 1, \ \ \ '为了标签中输出规整,判断数据是1位还是2位,分别空3个或2个空格 Label2 = Label2 & a(i, j) & s Next j

Label2 = Label2 & vbCrLf '换行 Next i Close #2 Case 2

max% = a(1, 1) For i = 1 To 4 For j = 1 To 4

If a(i, j) > max Then max = a(i, j) Next j Next i

Label3 = \最大值:\ Case 3

For j = 1 To 4

line3 = line3 + a(3, j) Next j

Label4 = \第三行元素之和:\ Case 4

Open App.Path & \ Print #3, max, line3 Close

MsgBox \已写入文件\ Case Else End End Select End Sub

41

'打开数据文件,准备追加数据

Open App.Path & \ '打开数据文件,准备读出 For i = 1 To 4 For j = 1 To 4

Input #2, a(i, j) '读磁盘文件中的一个数据,并赋值给数组元素

s$ = IIf(Len(CStr(a(i, j))) = 1, \ \ \ '为了标签中输出规整,判断数据是1位还是2位,分别空3个或2个空格 Label2 = Label2 & a(i, j) & s Next j

Label2 = Label2 & vbCrLf '换行 Next i Close #2 Case 2

max% = a(1, 1) For i = 1 To 4 For j = 1 To 4

If a(i, j) > max Then max = a(i, j) Next j Next i

Label3 = \最大值:\ Case 3

For j = 1 To 4

line3 = line3 + a(3, j) Next j

Label4 = \第三行元素之和:\ Case 4

Open App.Path & \ Print #3, max, line3 Close

MsgBox \已写入文件\ Case Else End End Select End Sub

41

'打开数据文件,准备追加数据

本文来源:https://www.bwwdw.com/article/q6fp.html

Top