《18个VB经典例题》由会员分享,可在线阅读,更多相关《18个VB经典例题(9页珍藏版)》请在金锄头文库上搜索。
1、-1、随机产生三个100300之间的整数,判断这三个整数是否能构成三角形,如果可以,求三角形的面积。Dim * As Integer, y As Integer, z As IntegerDim c As Single, s As Single* = Int(Rnd * 201 + 100)y = Int(Rnd * 201 + 100)z = Int(Rnd * 201 + 100)If * + y z And * + z y And y + z * Then c = (* + y + z) / 2 s = Sqr(c * (c - *) * (c - y) * (c - z) Print
2、三角形面积是: & s Else Print 构不成三角形End If2、随机产生0100之间的60名学生的数学分数,分别统计分数在、之间的学生人数。Dim a%, b%, c%, d%, e%, f%, g%, h%, i%, j%Dim * As SingleFor m = 1 To 60 产生60名学生的分数 * = Rnd * 101 产生0100的分数 Print *; If * = 90 Then a = a + 1 ElseIf * = 80 Then b = b + 1 ElseIf * = 70 Then c = c + 1 ElseIf * = 60 Then d = d
3、+ 1 ElseIf * = 50 Then e = e + 1 ElseIf * = 40 Then f = f + 1 ElseIf * = 30 Then g = g + 1 ElseIf * = 20 Then h = h + 1 ElseIf * = 10 Then i = i + 1 Else j = j + 1 End IfNe*t mPrint a, b, c, d, e, f, g, h, i, j3、我国有13亿人口,按照人口年增长0.8%计算,多少年以后我国人口超过26亿。Dim * As Double 人数一定要定义成双精度的Dim n As Integer* = 13
4、00000000n = 0Do While * = 0 Then *1 = (-b + Sqr(d) / (2 * a) 求方程的两个根 *2 = (b + Sqr(d) / (2 * a) *1 = Format(*1, .#) 对根规*化输出 *2 = Format(*2, .#)End IfPrint *1, *25、*次歌手大奖赛,共有10名选手,有10名评委打分。要求评委给10位选手打分,去掉一个最高分,去掉一个最低分,求出该选手的平均分。Dim i As IntegerDim j As IntegerDim ma* As Single, min As Single, sum As
5、Single, aver As SingleFor i = 1 To 10 十个选手的成绩 ma* = 0 min = 100 sum = 0 For j = 1 To 10 十个评委的分数 * = Rnd * 101 产生0100的分数 If * ma* Then ma* = * If * min Then min = * sum = sum + * Ne*t j aver = (sum - ma* - min) / 8 Print aver;Ne*t i6、求1到100以内的素数。Dim i As Integer, j As Integer, prime As BooleanFor i =
6、 1 To 100 prime = True For j = 2 To Sqr(i) 判断一个数是否是素数 If i Mod j = 0 Then prime = False End If Ne*t j If prime Then 如果prime = True则i是素数 Print i & 是素数 Else Print i & 不是素数 如果prime = False则i不是素数 End IfNe*t i7、求出100以内的所有勾股数勾股数为a2+b2=c2,c为自然数,且abDim a As Integer, b As Integer, c As IntegerFor a = 1 To 10
7、0 For b = 1 To 100 For c = 1 To 100 If c * c = a * a + b * b And a b Then Print a; b; c; Print End If Ne*t c Ne*t bNe*t a8、把输入的字符串逆序输出。Dim str As String, strRe As StringDim i As Integer str = Te*t1.Te*t 原字符串 For i = 1 To Len(str) strRe = Mid(str, i, 1) & strRe 字符串逆序 Ne*t i Te*t2.Te*t = strRe 逆序后的字符串
8、9、随机产生0100之间的60名学生的数学分数,分别统计分数在、之间的学生人数。用数组实现Dim i As Integer, s(1 To 10) As Integer, mark(1 To 60) As Integer,For i = 1 To 60 mark(i) =int( Rnd * 101) 随机产生0100之间的分数 Print mark(i); 输出60个分数 Select Case mark(i) Case Is 10 s(1) = s(1) + 1 Case Is 20 s(2) = s(2) + 1 Case Is 30 s(3) = s(3) + 1 Case Is 40
9、 s(4) = s(4) + 1 Case Is 50 s(5) = s(5) + 1 Case Is 60 s(6) = s(6) + 1 Case Is 70 s(7) = s(7) + 1 Case Is 80 s(8) = s(8) + 1 Case Is 90 s(9) = s(9) + 1 Case Is ma* Then ma* = mark(i) 找出最大数 If mark(i) mark(n + 1) Then mark(n + 2) = mark(n + 2) + 1Print mark(i); 输出学生成绩Ne*t iPrint mark(n + 1); mark(n + 2) 输出平均分与高于平均分的人数12、随机生成包含10个数组元素的有序数组,然后第一个与第六个进展交换,第二个与第七个进展交换。,并把原数组以及交换后的数组分别在窗体上显示出来。Dim s(1 To 10) As IntegerFor i = 1 To 10 产生10个数s(i) = Int(Rnd * 101)Print s(i); 输出交换前的数值Ne*t iPrintFor i = 1 To 5 实现交换t = s(i)s(i) = s(5 + i)s(5+ i) = tNe*