江苏师范大学科文学院VB编程题答案

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

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

1、 编写程序,求圆柱体的底面积、侧面积和体积。运行界面如图所示。

Command1

Private Sub Command1_Click() Const pi=3.1415926 Dim h, r As Integer h = Text1 r = Text2

Text3 =pi * r ^ 2 Text4 = 2 * pi * r * h Text5 = Val(Text3) * h End Sub

2、用条件语句实现:从文本框(Text1)输入一个数,单击“判断”按钮(Command1)判断它能否同时被3、5、7整除,若能整除,则用消息框(Msgbox函数)显示“能同时被3、5、7整除”,否则显示“不能整除”。

Private Sub Command1_Click() Dim m As Integer m = Text1

If m Mod 3 = 0 And m Mod 5 = 0 And m Mod 7 = 0 Then MsgBox \能同时被3、5、7整除\ Else

MsgBox \不能整除\ End If End Sub

3、使用循环控制结构在窗体上打印出下三角形式的九九乘法表。(界面如图所示)

Command1

Private Sub Command1_Click() For i = 1 To 9 For j = 1 To i

Print Str(i) & \ \ & \ & Str(j) & \ & Str(i * j) & Space(2); Next j Print Next i End Sub

4、编写程序,对输入的学生成绩给出五级评分。要求:学生成绩由文本框(Text1)输入,结果显示在标签(Label1)中。评分标准如下: 优 90≤成绩≤100 良 80≤成绩<90 中 70≤成绩<80 及格 60≤成绩<70 不及格 0≤成绩<60

Private Sub Command1_Click() Dim m As Integer m = Val(Text1) Select Case m

Case Is >= 90: Label1.Caption = \优\ Case Is >= 80: Label1.Caption = \良\ Case Is >= 70: Label1.Caption = \中\ Case Is >= 60: Label1.Caption = \及格\ Case Else: Label1.Caption = \不及格\ End Select End Sub 5、编写程序求

?k??kk?1k?1100502的值,要求结果显示在窗体上。

Private Sub Command1_Click() For i = 1 To 100 s = s + i Next i

For j = 1 To 50 m = m + j ^ 2 Next j

Sum = s + m Print Sum End Sub

6、随机生成15个100以内的正整数并显示在一个文本框(Text1)中,再将所有对称位置的两个数据对调后显示在另一个文本框(Text2)中(第1个数与第15个数对调,第2个数与第14个数对调,??,第7个数与第9个数对调)。 Option base 1

Private Sub Command1_Click() Text1 = \ Text2 = \

Dim a(15) As Integer For i = 1 To 15

a(i) = Int(101 * Rnd) Text1 = Text1 & Str(a(i)) Next i

For j = 1 To 15 If j < 8 Then t = a(j)

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

Text2 = Text2 & Str(a(j)) Next j End Sub

7、随机生成20个两位正整数显示在一个文本框(Text1)中,并求出它们的平均值显示在另一个文本框(Text2)中。 Option Base 1

Private Sub Command1_Click() Dim a(20) As Integer Text1 = \ Text2 = \ For i = 1 To 20

a(i) = Int(90 * Rnd + 10) s = s + a(i)

Text1 = Text1 & Str(a(i)) Next i

Text2 = s/20 End Sub

8、随机生成50个三位正整数显示在一个文本框中(Text1)中,并将它们逆序显示在另一个文本框(Text2)中。 Option Base 1

Private Sub Command1_Click() Dim a(50) As Integer Text1 = \ Text2 = \ For i = 1 To 50

a(i) = Int(900 * Rnd + 100) Text1 = Text1 & Str(a(i)) Next i

For j = 1 To 50 If j < 26 Then

t = a(j)

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

Text2 = Text2 & Str(a(j)) Next j End Sub 9、用InputBox函数输入10个数到数组A中,输入后将这10个数显示在一个文本框(Text1)中,并计算其中所有正数的和,将结果显示在另一个文本框(Text2)中。 Option Base 1

Private Sub Command1_Click() Dim a(10), s As Integer Text1 = \ Text2 = \ For i = 1 To 10

a(i) = Val(InputBox(\ Text1 = Text1 & Str(a(i)) If a(i) > 0 Then s = s + a(i) End If Next Text2 = s End Sub

10、生成100个[0,100]之间的随机整数存于数组A中,并将它们显示在一个文本框(Text1)中,在数组A中查找指定的元素Number。要求:Number用InputBox函数输入,查找结果显示在窗体上。 Option Base 1

Dim a(100), s As Integer

Private Sub Command1_Click() Dim number As Integer number = InputBox(\ Print a(number) End Sub

Private Sub Form_Load() Text1 = \

For i = 1 To 100

a(i) = Int(101 * Rnd) Text1 = Text1 & Str(a(i)) Next i End Sub 11、编程求由一位随机整数构成的4×5二维数组A的每一列元素的和。运行界面如图所示。

Option Base 1

Private Sub Command1_Click() Dim a(4, 5), s1 As Integer For i = 1 To 4 For j = 1 To 5

a(i, j) = Int(9 * Rnd + 1) Picture1.Print a(i, j); Next j

Picture1.Print Next i

For i = 1 To 5 s1 = 0

For j = 1 To 4

s1 = a(j, i) + s1 Next j

Text1 = Text1 & Str(s1) Next i End Sub

12、单击命令按钮command1时,生成包含[1,9]之间随机整数的两个5*5矩阵,分别显示在图片框Picture1和Picture2中,并求两个矩阵之和显示在图片框Picture3中。

Option Base 1

Private Sub Command1_Click()

Dim a(5, 5), b(5, 5), c(5, 5) As Integer For i = 1 To 5 For j = 1 To 5

a(i, j) = Int(9 * Rnd + 1) b(i, j) = Int(9 * Rnd + 1) c(i, j) = a(i, j) + b(i, j) Picture1.Print a(i, j); Picture2.Print b(i, j); Picture3.Print c(i, j);

Next j

Picture1.Print Picture2.Print Picture3.Print Next i End Sub

13、编写程序,在窗体上打印出如图所示的杨辉三角形。要求:行数由用户指定。

Option Base 1

Private Sub Command1_Click() Dim a(), n As Integer n = InputBox(\ ReDim a(n, n) For i = 1 To n For j = 1 To i

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

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

Print Format(a(i, j), \ Next j Print Next i End Sub 14、随机生成一个由两位正整数构成的5行5列矩阵显示在窗体上,并求出该矩阵中两条对角线元素之和。 Option Base 1

Private Sub Command1_Click() Dim a(5, 5), s1, s2 As Integer For i = 1 To 5 For j = 1 To 5

a(i, j) = Int(90 * Rnd + 10) Print a(i, j); If i = j Then

s1 = s1 + a(i, j) End If

If i + j = 6 Then s2 = s2 + a(i, j) End If Next j Print Next i Print s1, s2 End Sub 15、随机生成一个由两位正整数构成的6行6列矩阵显示在窗体上,并求出该矩阵中所有元素之和。

Option Base 1

Private Sub Command1_Click() Dim a(6, 6), s1 As Integer For i = 1 To 6 For j = 1 To 6

a(i, j) = Int(90 * Rnd + 10) Print a(i, j);

s1 = s1 + a(i, j) Next j Print Next i Print s1 End Sub

16、编写一个求1+2+3+?+n的函数,单击命令按钮(Command1)时调用该函数求1+2+3+4+5的值,结果显示在一个文本框(Text1)中。 Private Sub Command1_Click() Text1 = s(5) End Sub

Function s(n As Integer) As Integer For i = 1 To n s = s + i Next i End Function

17、编写程序,判断一个数N是否是奇数。要求使用通用过程。 Private Sub Command1_Click() Dim m As Integer, f As Boolean m = InputBox(\

Call s(m, f)

If f = True Then MsgBox \是奇数\ If f = False Then MsgBox \不是奇数\End Sub

Private Sub s(n As Integer, f As Boolean) If n Mod 2 = 1 Then f = True Else

f = False End If End Sub

18、编写求表达式

x3?y3?z3的值的Function过程。

Function ss(x, y, z As Integer) As Single ss = Sqr(Abs(x ^ 3 + y ^ 3 + z ^ 3)) End Function

19、编写一个摄氏温度与华氏温度转换的通用过程。在一个文本框(Text1)中输入摄氏温度,单击命令按钮command1时调用该通用过程在另一个文本框(Text2)中显示对应的华氏温度。摄氏温度(C)与华氏温度(F)转换的公式是:F?C?9?5?32。 Private Sub Command1_Click() Dim n As Integer, s As Single n = Val(Text1) Call f(n, s)

Text2 = Format(s, \End Sub

Private Sub f(m As Integer, s As Single) s = m * 9 / 5 + 32 End Sub

20、编写一个判断某正整数N是否是完数的通用过程。完数:一个数恰好等于它的因子之和(除自身),如:6=1+2+3,因此6是完数。 Private Sub s(n As Integer, f As Boolean) For i = 1 To n / 2

If n Mod i = 0 Then Sum = Sum + i End If Next i

If Sum = n Then f = True Else

f = False End If End Sub

21、编写一个求正整数m和正整数n的最大公约数的函数。

Function ff(m, n As Integer) As Integer Dim r As Integer r = m Mod n Do While r <> 0 m = n n = r

r = m Mod n Loop ff = n End Function

22、编写一个对数组元素进行排序的通用过程 Option Base 1

Private Sub s(b() As Integer) For i = 1 To UBound(b) - 1 For j = i + 1 To UBound(b) If b(i) > b(j) Then t = b(i) b(i) = b(j) b(j) = t End If Next j Next i

For i = 1 To 10

Text2 = Text2 & Str(b(i)) Next i End Sub

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

Top