admin 管理员组

文章数量: 888136


2024年1月10日发(作者:什么是css以及css的作用)

Vb编程题

1.求自然数1~100之间奇数之和。

Private Sub Form_Click()

Dim s As Long, i As Integer

s = 0

For i = 1 To 100 Step 2

s = s + i

Next

Print s

End Sub

2.求自然数1~100之间偶数之和。

Private Sub Form_Click()

Dim s As Long, i As Integer

s = 0

For i = 2 To 100 Step 2

s = s + i

Next

Print s

End Sub

3.编写程序,在窗体上随机输出一个10--99之间的两位整数。

Private Sub Form_Click()

cl

Randomize

Print Int(Rnd() * 90) + 10

End Sub

4.编程计算从200到1000之间的所有4的倍数之和。

Private Sub Form_Click()

cls

Sum = 0

For i = 200 To 1000 Step 4

Sum = Sum + i

Next

Print "200-1000中4的倍数和是:"; Sum

End Sub

5. 编程计算从300到1000之间的所有7的倍数之和。

Private Sub Form_Click()

Dim i As Integer, sum As Long

sum = 0

For i = 300 To 1000

If i Mod 7 = 0 Then

sum = sum + i

End If

Next

Print "300-1000中7的倍数和是:"; sum

End Sub

6.编写程序在窗体上计算:5/6+4/5+3/4+2/3+1/2

Private Sub Form_Click()

Dim s As Single, i As Integer, t As Single

s = 0

For i = 5 To 1 Step -1

t = i / (i + 1)

s = s + t

Next

Print s

End Sub

7.在窗体的单击事件中编写程序,从键盘上任意输入3个数,输出最大数。

Private Sub Form_Click()

Dim a As Integer, b As Integer, c As Integer

Dim max As Integer

a = Val(InputBox("请输入第一个数"))

b = Val(InputBox("请输入第二个数"))

c = Val(InputBox("请输入第三个数"))

Print a; b; c

max = a

If b > max Then max = b

If c > max Then max = c

Print "三数中最大数是:"; max

End Sub

8.在窗体的单击事件中编写程序,从键盘上任意输入3个数,输出中间数。

Private Sub Form_Click()

Dim a As Integer, b As Integer, c As Integer, t As Integer

Dim m As Integer

a = InputBox("请输入数据")

b = InputBox("请输入数据")

c = InputBox("请输入数据")

Print a; b; c

If a > b Then

t = a

a = b

b = t

End If

If b < c Then

m = b

ElseIf a > c Then

m = c

Else

m = a

End If

Print "中间数是"; m

End Sub

本题也可以将三个数排序,输出中间值。

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

Private Sub Form_Click()

Dim a As Integer, b As Integer, c As Integer

Dim min As Integer

a = Val(InputBox("请输入第一个数"))

b = Val(InputBox("请输入第二个数"))

c = Val(InputBox("请输入第三个数"))

Print a; b; c

min = a

If b < min Then min = b

If c < min Then min = c

Print "三数中最小数是:"; min

End Sub

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

Private Sub Form_Click()

cls

a = Val(InputBox("a="))

b = Val(InputBox("b="))

c = Val(InputBox("c="))

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 "按从小到大排序后的结果:"; a; "、"; b; "、"; c

End Sub

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

Private Sub Form_Click()

cls

Sum = 0

For i = 1 To 4

x = Val(InputBox("接收4个数"))

Sum = Sum + x

Next

Print "和是"; Sum

Print "平均值是"; Sum / 4

End Sub

12.在窗体的单击事件过程中,编写程序计算1!+2!+3!+ … +20!

方法一:

Private Sub Form_Click()

cls

Sum = 0

x = 1

For i = 1 To 20

x = x * i

Sum = Sum + x

Next

Print "1!+2!+...+20!="; Sum

End Sub

方法二:

Private Sub Form_Click()

cls

Sum = 0

For i = 1 To 20

x = 1

For j = 1 To i

x = x * j

Next

Sum = Sum + x

Next

Print "1!+2!+...+20!="; Sum

End Sub

13.有一个窗体,窗体中有一个文本框,名称为Text1,有一个按钮,试编写按钮的单击事件,实现计算1!+2!+3!+…+10!,将结果显示在文本框Text1中。例题4-21

Private Sub Command1_Click()

Dim sum As Long, i As Integer, x As Long

sum = 0

x = 1

For i = 1 To 10

x = x * i

sum = sum + x

Next

= "1!+2!+...+10!=" + Str(sum)

End Sub

14.编写程序计算:s=1+1/2!+1/3!+…+1/n!,说明:在窗体中有一个文本框控件:Text1,用来接收n的值,有一个标签控件:Label1,用来显示S的值,试编写窗体的单击事件,实现计算与显示功能,只要求写出事件的主要代码,事件过程的起始语句和结束语句可略去。例题4-22

Private Sub Command1_Click()

Dim sum As Single, i As Integer, x As Long

Dim n As Integer

n =

sum = 0

x = 1

For i = 1 To n

x = x * i

sum = sum + 1 / x

Next

n = "1+1/2!+...+1/" & n & "!=" & sum

End Sub

15.输入x,计算y的值。其中:y=1+3x (x>=0); y=1-5x (x<0)。

Private Sub Form_Click()

cls

x = Val(InputBox("x="))

If x >= 0 Then

y = 1 + 3 * x

Else

y = 1 - 5 * x

End If

Print "x="; x; "时y="; y

End Sub

16.编写程序,在窗体上输出由1、2、3、4、5这五个数字组成的所有可能的两位数,并统计它们的个数。

Private Sub Form_Click()

Cls

For i = 1 To 5

For j = 1 To 5

gs = gs + 1

Print i * 10 + j;

If gs Mod 5 = 0 Then Print

Next

Next

Print "共有"; gs; "个数。"

End Sub

17.通过文本框输入一个整数X,判断X是否能被2整除。判断的结果在标签中显示。

Private Sub Form_Click()

Dim x As Integer

x = Val()

If x Mod 2 = 0 Then

n = Str(x) + "能被2整除"

Else

n = Str(x) + "不能被2整除"

End If

End Sub

18.编写程序, 点击命令按钮Command1时,两个文本框text1与text2中的内容进行交换。

Private Sub Command1_Click()

t =

=

= t

End Sub

19、编写程序,交换两个变量当中的数据。

Private Sub Form_Click()

Cls

a = Val(InputBox("a="))

b = Val(InputBox("b="))

Print "交换前两个数:"; a; b

t = a: a = b: b = t

Print "交换后两个数:"; a; b

End Sub

20.求s=a+aa+aaa+…+aaa…aaa(n个a),其中a和n的值由用户自己输入。例如,当a=3,n=4时,S=3+33+333+3333。

方法一:

Private Sub Form_Click()

Cls

a = Val(InputBox("a="))

n = Val(InputBox("n="))

s = 0

s1 = 0

For i = 1 To n

s1 = s1 * 10 + a ' s1 = s1 + a * 10 ^ (i - 1)

s = s + s1

Next

Print s

End Sub

方法二:

Private Sub Form_Click()

Cls

a = InputBox("a=")

n = Val(InputBox("n="))

s = 0

For i = 1 To n

s = s + Val(String(i, a))

Next

Print s

End Sub

21.在窗体上输出下三角的九九乘法表。例题4-23

Private Sub Form_Click()

Cls

For i = 1 To 9

For J = 1 To i

Print i; "*"; J; "="; Format(Str(i * J), "!@@@@");

Next

Print

Next

End Sub

22.编写程序,将26个字母逆序输出。

Private Sub Form_Click()

Dim x As String, y As String, t As String

x = "abcdefghigklmnopqrstuvwxyz"

Print x

y = ""

For i = 26 To 1 Step -1

t = Mid(x, i, 1)

y = y + t

Next

Print y

End Sub

23.编写程序,将字符串"山高人为峰"按逆序"峰为人高山"输出。

Private Sub Command1_Click()

cls

s = "山高人为峰"

s1 = ""

For i = Len(s) To 1 Step -1

s1 = s1 & Mid(s, i, 1)

Next

Print s1

End Sub

24.设纸的厚度为0.5毫米,将纸对折,再对折…,求至少对折多少次,纸张的厚度能达到珠穆朗玛峰的高度8848米。

Private Sub Form_Click()

Cls

h = 0.5: cs = 0

Do While h < 8848000

h = h * 2

cs = cs + 1

Loop

Print "折纸次数为:"; cs

End Sub

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

Private Sub Form_Click()

Dim d As Integer

d = Val()

Select Case d

Case 0

n = "Sunday"

Case 1

n = "Monday"

Case 2

n = "Tuesday"

Case 3

n = "Wednesday"

Case 4

n = "Thursday"

Case 5

n = "Friday"

Case 6

n = "Saturday"

Case Else

MsgBox "输入数据错误"

End Select

End Sub

26.编写程序,要求随着不断单击命令按钮Command1,窗体的标题依次显示为:今天是星期一→今天是星期二→今天是星期三→今天是星期四→今天是星期五→今天是星期六→今天是星期日。

Private Sub Command1_Click()

Static i As Integer

i = i + 1

Select Case i

Case 1

n = "今天是星期一"

Case 2

n = "今天是星期二"

Case 3

n = "今天是星期三"

Case 4

n = "今天是星期四"

Case 5

n = "今天是星期五"

Case 6

n = "今天是星期六"

Case 7

n = "今天是星期日"

i = 0

End Select

End Sub

27.在文本框中输入时、分、秒,编写程序将它们换算为秒,然后输出。设小时为h,分为m,秒为s,则计算共有多少秒的公式为:x=h*3600+m*60+s。

Private Sub Command1_Click()

Dim h As Integer, m As Integer, s As Integer

Dim x As Long

h =

m =

s =

x = 3600 * h + m * 60 + s

= x

End Sub

28.在银行存款,计算本利和的公式为: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 Single, n As Integer, r As Single

Dim p1 As Single

p = 1000

n = 3

r = 0.02

p1 = p * (1 + r) ^ n

Print "3年后本利和="; Int(p1 * 100 + 0.5) / 100; "元"

End Sub

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

Private Sub Form_Click()

r = Val(InputBox("圆半径:"))

= "半径为" + Str(r) + "的圆球表面积是" + Str(4 *

3.14 * r)

= "半径为" + Str(r) + "的圆球体积是" + Str((3 /

4) * 3.14 * r ^ 3)

End Sub

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

Private Sub Form_Click()

cls

a = Val(InputBox("a="))

b = Val(InputBox("b="))

c = Val(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 "边长为:"; a; b; c; "的三角形面积为:"; area

End If

End Sub

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

Private Sub Form_Click()

Cls

r = Val(InputBox("r="))

Print "半径为"; r; "的圆"

Print "面积为"; Round(3.14 * r ^ 2, 2)

Print "周长为"; Round(2 * 3.14 * r)

End Sub

32.使用文本框输入长方形的长和宽,用MsgBox输出长方形的周长和面积.

Private Sub Form_Click()

Dim a As Single, b As Single

Dim s As Single, p As Single

a = Val()

b = Val()

s = a * b

p = 2 * (a + b)

MsgBox "面积是:" + Str(s) + Chr(10) + "周长是:" + Str(p)

End Sub

33.使用InputBox函数输入华氏温度,要求输出摄氏温度。公式为:C=(5/9)*(F-32), 输出要有文字说明,取两位小数。

Private Sub Form_Click()

Dim f As Single, c As Single

f = Val(InputBox("f="))

c = (5 / 9) * (f - 32)

Print "华氏度为:"; f; "摄氏度为:"; Round(c, 2)

End Sub

34.编写涨工资的程序。计算条件是:基本工资大于或等于1500元的,增加工资20%;若小于1500元大于或等于900元的,增加工资15%;小于900元的,增加工资10%。要求:(1)在文本框中输入基本工资值;(2)在标签上输出增长后的基本工资值;(3)当单击按钮command1时实现上述功能。

Private Sub Command1_Click()

gz = Val()

Select Case gz

Case Is >= 1500

gz = gz * (1 + 0.2)

Case Is >= 900

gz = gz * (1 + 0.15)

Case Else

gz = gz * (1 + 0.1)

End Select

n = Str(gz)

End Sub

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

Private Sub Form_Click()

Cls

s = Val(InputBox("通话时间"))

If s <= 3 Then

hf = 0.5

Else

hf = 0.5 + Int(s - 3) * 0.15

End If

Print "通话时间为"; s; "分种"

Print "应收话费"; Format(hf, "0.##"); "元"

End Sub

36.编程将一维数组A(10)中数组元素循环向后移动一个位置,最后一个元素移动到第一个元素的位置。

Private Sub Form_Click()

Dim a(10) As Integer

Randomize

cls

Print "初始数组为:"

For i = 0 To 10

a(i) = Int(Rnd * 10)

Print a(i);

Next

Print

t = a(10)

For i = 10 To 1 Step -1

a(i) = a(i - 1)

Next

a(0) = t

Print "移位后数组为"

For i = 0 To 10

Print a(i);

Next

Print

End Sub

37.编写程序,求s的值,s=1×2×3+2×3×4+…+n×(n+1)×(n+2)的值。n由程序输入。

Private Sub Form_Click()

Cls

n = Val(InputBox("n="))

s = 0

For i = 1 To n

s = s + i * (i + 1) * (i + 2)

Next

Print "1×2×3+2×3×4+…+"; n; "×"; n + 1; "×"; n + 2;

"="; s

End Sub

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

Private Sub Form_Click()

Cls

w = Val(InputBox("w="))

If w <= 50 Then

yf = w * 0.25

Else

yf = 50 * 0.25 + (w - 50) * 0.4

End If

Print "行李重量为"; w; "公斤"

Print "应付运费:"; Format(yf, "0.00"); "元"

End Sub

39.编写一个程序,用户连续输入N个值,直到输入非数值数据为止。最后输出其中最大的一个数。

Private Sub Command2_Click()

Cls

max = 0

Do

a = InputBox("a=")

Print a; " ";

If Not IsNumeric(a) Then

Exit Do

End If

If a > max Then max = a

Loop

Print

Print "最大数"; max

End Sub

40.编写程序,输入一个数,判定该数是否为素数,如果是素数,则输出“该数是素数”的信息,否则输出“该数不是素数”的信息。

Private Sub Form_Click()

Cls

x = Val(InputBox("x="))

For i = 2 To x - 1

If x Mod i = 0 Then Exit For

Next

If i = x Then

Print "该数是素数"

Else

Print "该数不是素数"

End If

End Sub

Private Sub Form_Click()

Cls

x = Val(InputBox("请输入数据"))

For i = 2 To Sqr(x)

If x Mod i = 0 Then Exit For

Next

If i > Sqr(x) Then

Print x; "是素数"

else

Print x; "不是素数"

End If

End Sub

41.编写程序,实现如下功能:由文本框Text1输入一个数,单击窗体时,如果该数大于等于0,将其平方根输出到标签Lable1上,否则输出“数据不能为负数”的提示信息。

Private Sub Form_Click()

x = Val(InputBox("接收一个数"))

If x >= 0 Then

n = Sqr(x)

Else

MsgBox "数据不能为负数"

End If

End Sub


本文标签: 输入 输出 计算 窗体