2015VB编程汇总2栏(含答案)

更新时间:2023-09-27 08:24:01 阅读量: 综合文库 文档下载

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

试题内容

一.顺序结构

1.使用InputBox函数输入华氏温度F,计算摄氏温度C:C=(5/9)*(F-32),然后用msgbox 函数输出结果,输出要有文字说明,取两位小数。 Private Sub Form_Click()

Dim f As Single, c As Single f = InputBox(\请输入华氏温度:\ c = (5 / 9) * (f - 32)

MsgBox \摄氏温度为:\& Format(c, \& \度\End Sub

2.在窗体上随机输出一个10--99之间的两位正整数。

Private Sub Form_Click() Randomize

Print Int(Rnd * (99 - 10 + 1) + 10) End Sub

3.交换两个文本框text1与text2中的内容 Private Sub Command1_Click() Dim t As String

t = Text1.Text

Text1.Text = Text2.Text Text2.Text = t End Sub

4.在Text1中输入一个3位自然数进行分解求和,其结果存入Text2中

Private Sub Command1_Click()

Dim n As Integer, a1 As Integer, a2 As Integer, a3 As Integer n = Text1.Text

a1 = n \\ 100

a2 = (n - a1 * 100) \\ 10 a3 = n - a1 * 100 - a2 * 10 Text2.Text = a1 + a2 + a3 End Sub

5.使用InputBox函数输入长方形的长和宽,计算并用MsgBox函数输出该长方形的周长和面积 Private Sub Form_Click()

Dim a As Single, b As Single, circ As Single, area As Single

a = InputBox(\请输入长方形的长a\ b = InputBox(\请输入长方形的宽b\ circ = 2 * (a + b) area = a * b

MsgBox \长方形的周长为:\vbNewLine & \长方形的面积为:\End Sub

6.交换两个变量a和b中的数据

Private Sub Form_Click() Dim a As Integer, b As Integer, t As Integer a = InputBox(\请输入a的值\ b = InputBox(\请输入b的值\ Print \、b数据交换前:\ t = a a = b b = t

Print \、b数据交换后:\End Sub

7. 若x=\,求x中的数字之和 Private Sub Command1_Click() Dim x As String x = \

Print Val(Mid(x, 3, 1)) + Val(Mid(x, 5, 1)) + Val(Mid(x, 9, 1)) End Sub

8.求边长为a的内切圆面积

Private Sub Form_Click()

Dim a As Integer, area As Single a = InputBox(\输入正方形边长a\ area = 3.14 * (a / 2) ^ 2

MsgBox \边长为\的内切圆面积为:\+ Format(area, \End Sub

9.使用InputBox函数输入圆半径r和圆柱高h, 使用文本框分别输出圆球表面积和圆球体积。提示:圆球表面积公式为4*3.14*r; 圆球体积公式为(3/4)*3.14*r3

Private Sub Command1_Click()

Dim r As Single, h As Single, area As Single, s As Single

r = InputBox(\输入圆半径r\ h = InputBox(\输入圆柱高h\ area = 4 * 3.14 * r

s = (3 / 4) * 3.14 * r ^ 3

Text1.Text = \圆球表面积:\ Text2.Text = \圆球体积:\

End Sub

10.编写程序,通过InputBox函数从键盘上输入4个数,计算并在窗体上显示这4个数的和及平

均值。 Private Sub Form_Click() Dim a As Integer, b As Integer, c As Integer, d As Integer

a = InputBox(\请输入数a\

b = InputBox(\请输入数b\

c = InputBox(\请输入数c\ d = InputBox(\请输入数d\ Print \个数的和:\

Print \个数的平均值:\4

End Sub

11.在银行存款,计算本利和的公式为:

p1=p(1+r)n,其中,p为存款数,n为年数,r 为年利率,p1为n年后本金和利息之和。设p=1000,n=3,r=0.02,编程计算3年后本利和。要求小数点后两位,四舍五入。 Private Sub Form_Click() Dim p As Integer, r As Single, n As Integer, p1 As Single p = 1000 n = 3 r = 0.02

p1 = p * (1 + r) * n

Print \本利和:\End Sub

12. 从键盘输入一个半径,输出圆的面积和周长,输出要求有文字说明,保留两位小数。

Private Sub Form_Click()

Dim r As Single, circ As Single, area As Single

r = InputBox(\输入圆半径\ circ = 2 * 3.14 * r area = 3.14 * r ^ 2

MsgBox \圆的周长为:\vbNewLine & \圆的面积为:\End Sub

二.选择结构:

1.任意输入2个数正整数a和b(a>b),求a除以b的商和余数

Private Sub Form_Click()

Dim a As Integer, b As Integer Start: a = InputBox(\请输入正整数a的值\被除数\

b = InputBox(\请输入正整数b的值\除数\If a < b Then

MsgBox \输入的两个正整数a和b应满足“a>b”\

Chr(13) & \请重新输入\

GoTo Start End If

Print \除以b的商为:\Print \除以b的余数为:\

End Sub 2.使用InputBox函数输入计算机成绩

x(0<=x<=100),判断其是优秀(90<=x<=100)、及格(60<=x<90)还是不及格(0<=x<60)

Private Sub Form_Click() Dim x As Single

x = InputBox(\请输入计算机成绩\成绩\If x >= 0 And x < 60 Then

MsgBox Str(x) & \成绩为\不及格\成绩等级\

ElseIf x >= 60 And x < 90 Then

MsgBox Str(x) & \成绩为\及格\成绩等级\

ElseIf x >= 90 And x <= 100 Then

MsgBox Str(x) & \成绩为\优秀\成绩等级\Else

MsgBox \成绩应在0~100之间\成绩\End If End Sub

3.计算快件重量W克的运费:计费标准每克为0.05元,但超过100克后,超出数每克为0.02元

Private Sub Command1_Click() Dim w As Single, f As Single w = Text1

If w <= 100 Then y = 0.05 * w Else

y = 0.05 * 100 + 0.02 * (w - 100) End If

Label4.Caption = Format(y, \元\End Sub

Private Sub Command2_Click() Unload Me End Sub

4. 用InputBox函数输入3个数据,判断这3个数据是否可以组成三角形,如果能够组成三角形,计算并在窗体上输出三角形的面积。构成三角形的条件是:任意两边之和大于第三边。面积公式为area= , 其中S=(A+B+C)/2,A、B、C为三条边的长度。

Private Sub Command1_Click()

Dim a As Single, b As Single, c As Single, s As Single, area As Single

a = InputBox(\请输入三角形的边长a\三角形边长\

b = InputBox(\请输入三角形的边长b\三角形边长\

c = InputBox(\请输入三角形的边长c\三角形边长\

If a + b > c And a + c > b And b + c > a Then s = (a + b + c) / 2

area = Sqr(s * (s - a) * (s - b) * (s - c)) Print \边长为\、\和\& Str(c) & \的\三角形面积为:\Else

MsgBox \输入的三边长不能构成三角形\提示信息\End If End Sub

5.输入年份y, 判断其是否为闰年

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

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

Label2.Caption = Str(y) & \年是闰年\Else

Label2.Caption = Str(y) & \年不是闰年\End If End Sub

Private Sub Command2_Click() Unload Me End Sub

6.通过文本框输入一个整数X,判断X是偶数还是奇数

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

If x Mod 2 = 0 Then

Label2.Caption = Str(x) & \为偶数\Else

Label2.Caption = Str(x) & \为奇数\End If End Sub

Private Sub Command2_Click() Unload Me End Sub

7.任意输入3个数a、b、c,求其中最小数

Private Sub Form_Click()

Dim a As Single, b As Single, c As Single a = InputBox(\请输入a值\b = InputBox(\请输入b值\c = InputBox(\请输入c值\Print \三个数为:\If a < b And a < c Then Print \最小数为:\Else

If b < c Then

Print \最小数为:\ Else

Print \最小数为:\ End If End If End Sub

8.任意输入3个数a、b、c,求其中最大数

Private Sub Form_Click()

Dim a As Single, b As Single, c As Single a = InputBox(\请输入a值\b = InputBox(\请输入b值\c = InputBox(\请输入c值\Print \三个数为:\If a > b And a > c Then Print \最大数为:\Else

If b > c Then

Print \最大数为:\ Else

Print \最大数为:\ End If End If End Sub

9. 在窗体的单击事件中编写程序,从键盘上任意输入3

个数,输出中间数。

Private Sub Form_DblClick()

Dim a As Single, b As Single, c As Single a = InputBox(\请输入a值\b = InputBox(\请输入b值\c = InputBox(\请输入c值\Print \三个数为:\If a < b Then

t = a: a = b: b = t End If

If a < c Then

t = a: a = b: b = t End If

If b < c Then

Print \中间数为:\Else

Print \中间数为:\End If End Sub

10.任意输入3个数a、b、c,升序排列并输出

Private Sub Form_Click()

Dim a As Single, b As Single, c As Single, t As Single

a = InputBox(\请输入a值\b = InputBox(\请输入b值\c = InputBox(\请输入c值\

Print \三个数原顺序为:\If a > b Then

t = a: a = b: b = t End If

If a > c Then

t = a: a = c: c = t End If

If b > c Then

t = b: b = c: c = t End If

Print \三个数的升序顺序为:\End Sub

11.编写程序,实现如下功能:使用InputBox函数输入一个数,单击窗体时,如果该数大于等于0,将其平方根输出到标签Lable1上,否则输出“数据不能为负数”

的提示信息。

Private Sub Form_Click() Dim x As Single

x = InputBox(\请输入一个数据\If x >= 0 Then

Label1.Caption = Format(Sqr(x)) Else

Label1.Caption = \数据不能为负\End If End Sub

12.函数计算:输入X的值,计算Y的值。当X>=0时,Y=1+X,X<0时,Y=1-2*X

Private Sub Command1_Click() Dim x As Single, y As Single x = Text1

y = IIf(x >= 0, 1 + x, 1 - 2 * x) Text2 = y End Sub

Private Sub Command2_Click() Unload Me End Sub

13.在窗体的单击事件中编写程序,从键盘输入3个数,按从小到大的顺序输出到窗体上。 Private Sub Form_Click()

Dim a As Single, b As Single, c As Single, t As Single

a = InputBox(\请输入a值\b = InputBox(\请输入b值\c = InputBox(\请输入c值\

Print \三个数原顺序为:\If a > b Then

t = a: a = b: b = t End If

If a > c Then

t = a: a = c: c = t End If

If b > c Then

t = b: b = c: c = t End If

Print \三个数的升序顺序为:\End Sub

14.在窗体的单击事件中编写程序,要求:由文本框TEXT1输入一个0到6的数字,单击窗体时,在标签控件LABEL1上显示汉英对照星期信息,如:输入0,则显示星期日(SUNDAY)、输入1,则显示星期一(MONDAY),以此类推。如果输入0--6以外的数据,用MsgBox信息框显示“输入数据错误”的提示信息。Private Sub Form_Click() Dim m As Integer

m = Text1 Select Case m Case 0

Label1.Caption = \星期日(SUNDAY)\ Case 1

Label1.Caption = \星期一(MONDAY)\ Case 2

Label1.Caption = \星期二(TUESDAY)\ Case 3

Label1.Caption = \星期三(WEDNESDAY)\ Case 4

Label1.Caption = \星期四(THURSDAY)\ Case 5

Label1.Caption = \星期五(FRIDAY)\ Case 6

Label1.Caption = \星期六(SATURDAY)\ Case Else

MsgBox \输入数据错误\End Select End Sub

15. 编写涨工资的程序。计算条件是:基本工资大于或等于1500元的,增加工资20%;若小于1500元大于或等于900元的,增加工资15%;小于900元的,增加工资10%。要求:(1)在文本框中输入基本工资值;(2)在标签上输出增长后的基本工资值;(3)当单击按钮command1时实现上述功能。 Private Sub Command1_Click() Dim w As Single, s As Single w = Text1 Select Case w Case Is >= 1500 s = w + 0.2 * w Case Is >= 900 s = w + 0.15 * w Case Else

s = w + 0.1 * w End Select

Label3.Caption = Format(s, \

End Sub

Private Sub Command2_Click() Unload Me End Sub

16. 公用电话收费标准如下:通话时间在3分钟之内(包括3分钟),收费0.5元;3分钟以上,则每超过1分钟加收0.15元。编写一段程序,计算某人通话S分钟,应缴多少电话费。

Private Sub Command1_Click() Dim p As Single, m As Single m = Text1

If m <= 3 Then p = 0.5 Else

p = 0.5 + 0.15 * (m - 3) End If

Label4.Caption = Format(p, \元\End Sub

Private Sub Command2_Click() Unload Me End Sub

17. 火车站托运行李,按规定当行李重量不超过50公斤时,每公斤运费0.25元,超过50公斤后,超过部分按每公斤0.40元收费。输入行李重量W公斤,计算出应付多少运费。

Private Sub Command1_Click() Dim w As Single, f As Single w = Text1

If w <= 50 Then f = 0.25 * w Else

f = 0.25 * 50 + 0.4 * (w - 50) End If

Label4.Caption = Format(f, \元\End Sub

Private Sub Command2_Click() Unload Me End Sub

18.求解一元二次方程 ax2+bx+c=0 (a≠0)。 Private Sub Command1_Click()

Dim a As Single, b As Single, c As Single Dim d As Single, x1 As Single, x2 As Single a = Text1: b = Text2: c = Text3 d = b ^ 2 - 4 * a * c If d >= 0 Then

x1 = (-b + Sqr(d)) / (2 * a) x2 = (-b - Sqr(d)) / (2 * a)

Label4.Caption = \ Label5.Caption = \Else

x1 = -b / (2 * a): x2 = Sqr(Abs(d)) / (2 * a) Label4.Caption = \\

Label5.Caption = \\End If End Sub

Private Sub Command2_Click()

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

Top