1、顺序结构: Dim var1 As Integer, var2 As String var1 = 12345 var2 = InputBox("输入Var2=", "输入对话框") MsgBox(var2 & Chr(13) + Chr(10) & "var1=" & Str(var1)) 'MsgBox(var2 & vbCrLf & "var1=" & Str(var1)) 赋值类型 一、模仿第1题的实验,通过编程运行,理解变量、表达式和函数的定义、书写规则。 (1)在变量说明语句中,将变量A、变量B…
2、…变量J分别说明成什么类型,才能与下列的值匹配。 325、546.2、5383149、2003-12-31、“ABCDEF”、 false 、“123456”、 3.2434E+100、1.2563E+12 Dim a As Short Dim b As Single Dim c As Integer Dim d As Date Dim e1 As String Dim f As Boolean Dim g As String Dim h As Doubl
3、e Dim i As Single Dim j(4, 3) As Integer a = 3.25 b = 546.2 c = 5383149 d = #12/31/2003# e1 = "ABCDEF" f = False g = "123456" h = 3.24E+100 i = 1.2563E+15 j(0, 0) = 456 MsgBox(a & v
4、bCrLf & b & vbCrLf & c & vbCrLf & d & vbCrLf & e1 & vbCrLf & f & vbCrLf & g & vbCrLf & h & vbCrLf & i & vbCrLf & j(0, 0)) 二、分析下列表达式的运算结果,在窗体单击事件过程中,通过定义与运算结果类型相符合的变量,并将运算结果赋值给已定义的对应变量。观察程序编写过程中的语法编译提示。 1) 2) + |0.123-3| 3) - sin(300 ) 4) e6+ln( +2.135) 5) 89&12<>²8912 6)
5、 "AB"+STR(32.56)+ "CD" 7) mid("student”,5)= “ent” And(5>2) 8) Not (6>2) Or (7<9)) Dim x21, x22, x23, x24 As Single Dim x25, x27, x28 As Boolean Dim x26 As String x21 = Sqrt(3 ^ 2 / (2 * 4)) x22 = +Abs(0.123 - 3) x23 = -Sin(30 * PI / 180)
6、 x24 = Exp(6) + Log(2.135) x25 = 89 & 12 <> 8912 x26 = "AB" + Str(32.56) + "CD" 'x27 = Mid("student", 5) = "ent" And (5 > 2) x27 = Microsoft.VisualBasic.Right("student", 3) = "ent" And (5 > 2) x28 = Not (6 > 2) Or (7 < 9) MsgBox(x21 & vbCrLf
7、 & x22 & vbCrLf & x23 & vbCrLf & x24 & vbCrLf & x25 & vbCrLf & x26 & vbCrLf & x27 & vbCrLf & x28) 三、 1、产生一个100以内的随机整数 Dim y As Integer y = Int(Rnd() * 100) MsgBox(y) 2、给出任意字符串,测其长度 Dim y As String Dim y1 As Integer y = "VB程序设计语言" y1 = Len(y)
8、 MsgBox(y & "长度为" & y1) 3、在给出的字符串中找到stu的位置 Dim y As Integer y = InStr("abc student", "stu") MsgBox(y) 4、去除字符串" Visual C ++ "中两端的空格 Dim y As String y = Trim(" Visual C ++ ") MsgBox("去除左边空格" & y & "去除右边空格") 5、测字符“我去资料室查资料”所占字节 Private Sub B
9、utton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim y As String Dim y1 As Integer y = "VB程序设计语言" y1 = Len(y) * 2 MsgBox(y & " 字节数为" & y1) End Sub 6、将字符串“料资查室料资去我”反序排列 Private Sub Button8_Click(ByVal
10、 sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Dim y, y1 As String y = "我去资料室查资料" y1 = StrReverse(y) MsgBox(y1) End Sub 7、将“我去资料室查资料”截取“资料室”三字 Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventAr
11、gs) Handles Button9.Click Dim y, y1 As String y = "我去资料室查资料" y1 = Mid(y, 3, 3) MsgBox(y1) End Sub 8、将“我去资料室查资料”截取右边的“资料”两字 Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click Dim y, y1 A
12、s String y = "我去资料室查资料" y1 = Mid(y, 7, 2) 'y1 = Microsoft.VisualBasic.Right(y, 2) MsgBox(y1) End Sub 9、在字符串“Thisisabook.”中加空格成句 Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click Dim y, y
13、1 As String y = "Thisisabook" y1 = Mid(y, 1, 4) & Space(1) & Mid(y, 5, 2) & Space(1) & Mid(y, 7, 1) & Space(1) & Mid(y, 8) MsgBox(y1) End Sub 10、用函数求字母“Z”的ASCⅡ码 Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12
14、Click Dim y As Integer y = Asc("Z") MsgBox(y) End Sub 11、用函数计算2010年1月9日与你做实验的时间相隔多少年、月、日 Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click Dim y As Date Dim y1, y2, y3 As Integer
15、 y = "2013-1-1" y1 = DateDiff(DateInterval.Month, y, Today()) y2 = DateDiff(DateInterval.WeekOfYear, y, Today()) y3 = DateDiff(DateInterval.Day, y, Today()) MsgBox(y1 & "个月" & y2 & "个星期" & y3 & "天") End Sub 12、将字符串“42897”转化为数值 Private Sub Button14_Click
16、ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click Dim y As String Dim y1 As Integer y = "42897" y1 = Val(y) MsgBox(y1) End Sub 13、将3287.153用输出格式000.00表示 Private Sub Button15_Click(ByVal sender As System.Object, B
17、yVal e As System.EventArgs) Handles Button15.Click Dim y As Single y = 3287.153 MsgBox(Format(y, "000.00")) End Sub 在窗体单击事件中,利用MsgBox函数和格式输出函数输出以下数据。要求:①每行显示4个数据,每个数据隔4个空格;②数值型数据按“00000.00”数值格式输出,字符型数据按占10位字符宽度格式输出,日期型数据按“d”日期格式输出。(提示:用MsgBox函数来显示多行数据时,可以利用Chr(13)和
18、Chr(10)实现回车换行的效果) 45.632 ,8246.25 ,123.8 ,45627 ,“student”,“permanence”,“teachers”,#4/5/2004# Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click Dim z1, z2, z3, z4 As Single Dim z5, z6, z7 As String Dim z8
19、As Date z1 = 45.632 z2 = 8246.25 z3 = 123.8 z4 = 45627 z5 = "student" z6 = "permanence" z7 = "teachers" z8 = #4/5/2004# MsgBox(Format(z1, "00000.00") & " " & Format(z2, "00000.00") & " " & Format(z3, "00000.00") & " " &
20、 Format(z4, "00000.00") & vbCrLf & Format(z5, "{0,10}") & " " & Format(z6, "{0,10}") & " " & Format(z7, "{0,10}") & " " & Format(z8, "d")) End Sub 1、求 x1 = -b + 根号下(b ^ 2 - 4 * a * c) / 2 / a x2 = -b - Math.Sqrt(b ^ 2 - 4 * a * c) / 2 / a的值 Public Class Form1 Private Sub B
21、utton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As String, b As String, c As String Dim x1 As Single, x2 As Single a = InputBox("输入A", "输入数据Y", , 500, 500) b = InputBox("输入B", "输入数据Y", , 500, 500) c = I
22、nputBox("输入C", "输入数据Y", , 500, 500) x1 = -b + Math.Sqrt(b ^ 2 - 4 * a * c) / 2 / a x2 = -b - Math.Sqrt(b ^ 2 - 4 * a * c) / 2 / a MsgBox("x1=" & Format(x1, "###.###") & " x2=" & Format(x2, "###.###")) End Sub 2、求(x ^ 3 + x * y + y ^ 3) / (2 * x * y)的值,其中x=5,
23、y=9 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim x As Integer Dim y As Integer Dim z As Single x = 5 y = 9 z = (x ^ 3 + x * y + y ^ 3) / (2 * x * y) MsgBox("(x ^ 3 + x *
24、 y + y ^ 3) / (2 * x * y)=" & z) End Sub 3、计算y=ln20+|x-16| Dim x As Single Dim y As Single x = InputBox("输入x") y = Math.Log(20) + Math.Abs(x - 16) MsgBox("Math.Log(20) + Math.Abs(x - 16)=" & y) 4、y=0.231x+1.36 y = 0.231 * x + 1.36 5、随机产生一个正整数,判断并输出是“
25、偶数”还是“奇数” Dim x As Integer x = Int(Rnd() * 100) If x Mod 2 = 0 Then MsgBox(x & "是偶数") Else MsgBox(x & "是奇数") End If 6、在a b c 中找出最大数和最小数 Dim a As Single, b As Single, c As Single, x As Single, y As Single a = InputBox("请输入a")
26、 b = InputBox("请输入b") c = InputBox("请输入c") If a > b Then x = a y = b Else x = b y = a End If If c > a Then x = c Else If c < b Then y = c
27、 End If End If MsgBox("最大数" & x & " 最小数" & y) 7、计算f=0.06t+2 (t<120) 0.06t*0.85(t>=120) Dim t As Single, f As Single t = InputBox("请输入t") If t < 120 Then f = 0.06 * t + 2 MsgBox("f = 0.06 * t + 2=" & f) El
28、se f = 0.06 * t * 0.85 MsgBox("f = 0.06 * t * 0.85=" & f) End If 8、根据成绩划分等级 Dim x As Single Dim y As String x = InputBox("请输入成绩") If x >= 90 Then y = "A" ElseIf x >= 80 Then y = "B" ElseIf x
29、 >= 70 Then
y = "C"
ElseIf x >= 60 Then
y = "D"
Else
y = "E"
End If
MsgBox("成绩" & x & "等级为" & y)
End Sub
9、SELECT CASE …… END SELECT语句计算
y=x(x+2) (0 30、As Single
x = InputBox("请输入x")
Select Case x
Case Is <= -1
y = x - 1
Case Is <= 2
y = 2 * x
Case Is <= 10
y = x * (x + 2)
Case Else
y = 0
End Select
31、 MsgBox("y=" & y)
End Sub
循环结构
1、ForNext语句计算 + + …..
Dim i As Integer
Dim n As Integer
Dim s As Single
s = 0
n = InputBox("请输入n")
For i = 1 To n
s = s + 1 / (i * (i + 1))
Next
MsgBo 32、x("s=" & s)
End Sub
2、利用Inputbox函数给一个10元素的数组赋10个任意数据类型的值,分别计算下标为奇数的元素之和、下标为偶数的元素之和
Dim a(10) As Integer
Dim i As Integer
Dim x As Integer
Dim y As Integer
x = 0
y = 0
For i = 1 To 10
a(i) = InputBox("请输入第" & i & "个数")
33、 If i Mod 2 = 0 Then
x = x + a(i)
Else
y = y + a(i)
End If
Next
MsgBox("下标为奇数的元素和为" & y & "下标为偶数的元素和为" & x)
End Sub
3、等腰三角形
Dim r As Integer, s As Integer, c As Integer, y(5) As String, j As Integer, ys As S 34、tring
ys = ""
r = 1
Do While r <= 4
c = 10 - r : s = 1
y(r) = y(r) & Space(c + s)
Do While s <= 2 * r - 1
y(r) = y(r) & "*"
s = s + 1
Loop
r = r + 1
Loop
Fo 35、r j = 1 To 4
ys = ys & y(j) & Chr(10)
Next
MsgBox(ys)
End Sub
4、直角三角形
Dim r As Integer, s As Integer, c As Integer, y(5) As String, ys As String
ys = ""
r = 4
Do While r >= 1
c = 10 - r : s = 1
ys = ys & Spa 36、ce(c + s)
Do While s <= 2 * r - 1
ys = ys & "*"
s = s + 1
Loop
r = r - 1
ys = ys & Chr(10)
Loop
MsgBox(ys)
End Sub
5、产生20个0~100的随机数,并统计<10和>50的个数
Dim a(19) As Integer
Dim s As St 37、ring
Dim i As Integer
Dim x As Integer
Dim y As Integer
x = 0
y = 0
s = ""
For i = 0 To 19
a(i) = Int(Rnd() * 100)
s = s & a(i) & ","
If a(i) > 50 Then
x = x + 1
Else 38、If a(i) < 10 Then
y = y + 1
End If
Next
MsgBox(s & vbCrLf & "50万元以上的业务人数为" & x & ",10万元以下的业务人数为" & y)
End Sub
6、产生20个0~100的随机数,并将其从大到小排序
Dim a(19) As Integer
Dim t As Integer
Dim i As Integer
Dim j As Integer
39、 Dim x As String
Dim y As String
x = ""
y = ""
For i = 0 To 19
a(i) = Int(Rnd() * 100)
x = x & a(i) & ","
Next
For i = 0 To 19
For j = 19 To i + 1 Step -1
If a(j) < a(j - 1) Then
40、
t = a(j)
a(j) = a(j - 1)
a(j - 1) = t
End If
Next
y = y & a(i) & ","
Next
MsgBox(x & vbCrLf & y)
End Sub
7、用随机函数和取整函数产生一个8位数的整数,判断0~9在其中出现的次数
Dim a As Integer
41、
Dim b(9) As Integer
Dim s As Integer
Dim u As String
Dim i As Integer
Randomize()
a = Int(Rnd() * 90000000 + 10000000)
For i = 0 To 7
s = Mid(a, i + 1, 1)
b(s) = b(s) + 1
Next
u = "8位数为 42、" & a & ")" & vbCrLf
For i = 0 To 9
u = u & i & "出现次数:" & b(i) & vbCrLf
Next
MsgBox(u)
End Sub
8、随机产生20个八位整数
Dim b(19) As Integer
Dim i As Integer
Dim s As String
s = ""
For i = 0 To 19
b(i) = Int(R 43、nd() * 90000000 + 10000000)
s = s & b(i) & vbCrLf
Next
MsgBox(s)
End Sub
9、 Do While … Loop 语句计算
Dim i As Integer
Dim t As Single
Dim s As Single
i = 1
t = 1 / (i * (i + 1))
s = 0
Do While t >= 0.000001
44、
s = s + t
i = i + 1
t = 1 / (i * (i + 1))
Loop
MsgBox("s=" & s)
End Sub
自定义函数
1、 在InputBox中输入n,求Y=2!+4!+6!+8!+……n!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle 45、s Button1.Click
Dim n As Integer
n = InputBox("请输入需求阶乘之和的任意整数n")
If n Mod 2 = 0 Then
MsgBox("偶数序列" & Str(n) & " 的阶乘之和:" & Str(factorial(n)))
Else
MsgBox("请输入偶数")
End If
End Sub
Public Function factorial(ByVal x) As In 46、teger
Dim s As Integer
Dim t As Integer
Dim i As Integer
Dim j As Integer
s = 0
For i = 1 To x Step 2
t = 1
For j = 1 To i
t = t * j
Next
s = s + t
Next
Re 47、turn s
End Function
2、已知半径、高,求圆面积、圆周长、圆柱体积
Dim r As Single
Dim h As Single
r = InputBox("请输入半径:")
h = InputBox("请输入高度:")
Formula(r, h)
End Sub
Public Sub Formula(ByVal r As Single, ByVal h As Single)
Dim x As Single
Dim y As 48、Single
Dim z As Single
x = Math.PI * r ^ 2
y = 2 * Math.PI * r
z = Math.PI * r ^ 2 * h
MsgBox("圆面积:" & x & ",圆周长:" & y & ",圆柱体积:" & z)
End Sub
3、将输入的字符串颠倒顺序
Dim s As String
s = InputBox("请输入一个字符串")
MsgBox("" & Inverse(s))
49、
End Sub
4、通过递归形式计算
Dim m As Integer
Dim n As Integer
Dim s As Integer
m = InputBox("请输入一个数m")
n = InputBox("请输入一个数n")
s = cmn(m, n)
MsgBox("该组合数为" & s)
End Sub
Public Function cmn(ByVal m As Integer, ByVal n As Integer) A 50、s Integer
If n = 0 Then
Return 1
ElseIf n = 1 Then
Return m
ElseIf n > m / 2 Then
Return cmn(m, m - n)
Else
Return cmn(m - 1, n) + cmn(m - 1, n - 1)
End If
End Function
窗口控件
1、 计算汽油升数
汽油升数:La






