VB程序题目

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

程序改错
1 题目:求s=2!+4!+6!+8!,阶乘的计算用Function过程fact实现。

程序代码:
Private Sub Form_Click()
Dim i as integer, s as long
For i = 2 to 8 →For i = 2 to 8 step 2
s = s + fact(i)
Next i
Print s
End Sub
Public Function fact( ) →Public Function fact(Byval n%)
Dim t as long
Dim i as integer
t = 1
For i = 1 to n
t = t * i
Next i
Fact = i →Fact = t
End function
2 题目:请根据下列描述编写购物优惠程序。

某商场为了加速促成商品流通,采用购物打折的优惠办法,每位顾客一次购物(1)在100元以上者,按九五折优惠;(2)在200元以上者,按九折优惠;(3)在300元以上者,按八折优惠;(4)在500元以上者,按七折优惠。

在窗体上添加两个文本框和一个命令按钮,要求在Text1中输入购物商品总金额,单击命令按钮,在Text2中输出优惠价。

程序代码:
Private Sub Command1_Click ()
Dim x As Integer, y As Single
x = Val(Text1.Text)
If x < 100 then
x = y →y=x
Else
If x < 200 then
y = 0.95 * x
Else
If x < 300 then
y = 0.9 * x
Else
If x < 300 then
y = 0.8 * x
Else
y = 0.7 * x
Else If →End If
End If
End If
Text2.text = x →Text2.text = y
End Sub
程序填空
1 题目:由输入对话框输入n(n必须为大于零且小于30的自然数),计算下列表达式的值,并在标签框label1上显示。

Sum=1/2+1/6+……+1/(k*(k+1)) (k<=n)
Private sub form_click()
Dim n as integer, sum as double, k as integer
N = val(inputbox(“n=”,”请输入自然数n(n>0且n<30)”))
Do while n<=0 orn>30
N = val(inputbox(“n=”,”请重新输入”))
Loop
Sum = 0
K = 0
Do
K= k+1
Sum = sum + 1/(k * (k+1))
Loop until (k+1) > n
Label1.caption = “sum” + str(sum)
End sub
2 题目:下面的程序用于打印出以下图形。

程序代码:
Private sub form_click()
Dim i%, j%, k%
For I = 3 to 0 step -1
For j = 0 to 2 * i
Print “ ”;
Next j
For k = 5 to 11-2*i
Print “*” ;
Next k
print
Next i
For I = 0 to 3
For j = 0 to 2 * I + 2
print “ ”;
For k = 0 to 4-2 * i
Print “*” ;
Next i
print
Next i
End sub
程序设计
1 题目:求二维数组中最大元素及其所在的行和列,并将最大值存入变量Max中,将最大值的行位置存入row中,列位置存入变量column中
程序代码:
Private Sub Form_load()
Show
Dim a(2,3) As Integer
Dim max As Integer, row As Integer, column As Integer
a(1,1) = 34 : a(1,2) = 34 : a(1,3) = 43
a(2,1) = 34 : a(2,2) = 78 : a(2,3) = 12
Max = a(1,1)
Row = 1
Column =1
For i = 1 to 2
For j = 1 to 3
If a(i,j) > max Then max=a(i,j) : row = i : column = j
Next j
Next i
Print max,row,column
End Sub
2 题目:(事件)双击窗体(响应)求3+6+9+12+15+18+……+300的和,并将结果输出到窗体上。

将结果存入变量sum中。

程序代码:
Private Sub Form_DblClick()
Dim sum As Integer
Sum = 0
For j = 3 to 300 step 3
Sum = sum + j
Next j
Print sum
End Sub
3 题目:创建一个绘图程序。

当程序运行时,单击“绘图”按钮,在窗体的Picture控件上画出【-pi,pi】即【-3.14,3.14】区间的余弦函数曲线。

要求将图片框的坐标系定义为左上角为(-4,2),右下角为(4,-2),并在图片框中画出坐标的x轴和y轴。

图例:
程序代码:
Private Sub Command1_Click()
Dim i As Integer →Dim i As single
Picture1.Scale (4,2)-(4,2) →Picture1.Scale (-4,2)-(4,-2)
Picture1.Line (-4,0)-(4,0)
Picture1.Line (0,2)-(0,-2)
Picture1.CurrentX = 0.2
Picture1.CurrentY = -0.1
Picture1.Print “0”
Picture1.CurrentX = -3.2
Picture1.CurrentY = -0.1
Picture1.Print “-n”
Picture1.CurrentX = 3.3
Picture1.CurrentY = -0.1
Picture1.Print “n”
Picture1.CurrentX = 0.2
Picture1.CurrentY = 0.9
Picture1.Print “1”
Picture1.CurrentX = 0.2
Picture1.CurrentY = -0.8
Picture1.Print “-1”
For i = -3.141 To 3.141 Step 0.001
Picture1.Pset (i,sin(i)) →Picture1.Pset (i,cos(i))
Next i
End Sub
Private Sub Command2_Click()
End
End Sub
4 题目:用自定义函数的方法求sum(x)。

当x在[-1,1]上,sum(x)=x/2!+x^2/3!+x^3/4!+……+x^n/(n+1)!;当x>1或x<-1时,sum(x)=0。

当n<=0时,输入数据错误。

x,n都是由用户输入。

程序代码:
Private Sub Command1_Click()
Dim s AS Single
Dim n As Integer, x As Single, k As Integer
n = Val(Inputbox(“Please input a integer value:”))
x = Val(Inputbox(“Please input a single value:”))
if n <= 0 then
k = MsgBox(“数据输入错误!”,vbRetryCancel,”数据输入”)
Exit Sub
End If
s =sum(x,n)
Print s
End Sub
Function Sum(x As Single, n As Integer)
Dim i As Integer, ss As Long
ss = 1
sum = 0
If x > 1 Or x < -1 Then
Exit →sum = 0
Else
For i = 2 To n →For i = 2 To n+1
ss = ss * i
sum = x ^ (i - 1) /ss →sum =sum + x ^ (i - 1) /ss
Next i
End if
End Function
程序填空
3 题目:以下程序段用于实现:输入两个正整数m和n,求其最大公因数和最小公倍数。

程序代码:
Private sub form_click ()
Dim a%, b%, num1%, num2%, temp
Num1 = inputbox(“请输入一个正整数”)
Num2 = inputbox(“请输入一个正整数”)
If a<b then
Temp = num1 : num1 = num2 : num2 = temp
End if
A = num1
B = num2
Do while a mod b <> 0
Temp = a mod b
A = b
b = temp
Loop
Print “最大公约数为:”; b
Print ”最小公倍数为:”; num1 *num2 /b
End sub
4 题目:窗体上建立一个文本框text1,用于显示结果。

单击按钮弹出输入对话框,录入0-6之间的数值,判断输出值为星期几。

程序代码:
Private sub command1_click()
Dim x as integer
X = val( inputbox (“请输入星期几(0~6)”))
Select case x
Case is = 0
Text1.text = “星期日”
Case is = 1
Text1.text = “星期1”
Case is = 2
Text1.text = “星期2”
Case is = 3
Text1.text = “星期3”
Case is = 4
Text1.text = “星期4”
Case is = 5
Text1.text = “星期5”
Case is = 6
Text1.text = “星期6”
Case else
Text1.text = “输入错误”
End select
End sub
程序设计
3 题目:(事件)单击窗体(响应)求100以内偶数的和,并将结果输出在窗体上。

将结果存入变量sum中。

程序代码:
Private Sub Form_Click()
Dim sum As Integer
Sum=0
For j = 2 To 100 Step 2
Sum=sum + j
Next j
Print sum
End Sub
4 题目:(事件)单击窗体(响应)求10!的值。

并将结果输出到窗体上。

将结果存入变量sum中。

Private Sub Form_Click()
Dim sum As Long
Sum = 1
For j = 1 To 10 do
Sum = sum * j
Next j
Print sum
End Sub
程序改错
5 题目:请修改程序中的错误,保证程序最后输出效果如图1所示。

图例:
程序代码:
Private Sub Form_Click()
Dim i As Integer
For i = 1 To 7
Print Spc(7); →Print Spc(7-i);
If i >= 1 Then →If i <= 1 Then
Print “*”
Else
Print “*”;Spc(2*(i - 1));”*”→Print “*”;Spc(2*(i-1)-1);”*”
End If
Next i
End Sub
6 题目:输入一个字符串,将所有的小写字母都转换为大写字母。

程序代码:
Private Sub Form_Click()
Dim s1 As String, s2 As String, i As Integer, t As String
s1 = Inputbox(“输入字符串,以#号结束”)
i = 1
s1 = Mid (s1,i,1) →t = Mid (s1,i,1)
While t <> “#”
If t >= “a” And t <= “z” Then t = Chr(Asc(t)-30) →If t >= “a” And t <= “z” Then t = Chr(Asc(t)-32)
s2 = s2 +t
i = i +1
t = Mid(s1,i,1)
Print “新字符串为:”;s1 →Print “新字符串为:”;s2
End Sub
程序填空
5 题目:下面的时间过程判断文本框text1中输入的数所在区间,并在文本框text2中输出判断结果。

程序代码:
private sub command1_click()
Dim int1 as integer
int1 = val(text1.text)
Select case int1
Case 0
Text2.text = “值为0”
Case 1 to 10
Text2.text = “值在1和10 之间(包括1和10)”
Case Is > 10
Text2.text = “值大于10”
Case else
Text2.text = “值小于0”
End select
End sub
程序设计
5 题目:(事件)单击窗体。

(响应)求1!+2!+3!+4!+5!并将结果输出到窗体上。

结果存入变量S中。

程序代码:
Private sub form_click()
Dim s As Integer
S = 0
For i = 1 To 5
x= 1
For j = 1 To i
x = x *j
Next j
S = s + x
Next i
Print s
End Sub
6题目:(事件)单击窗口(响应)求1-1/2+1/3-1/4+……+1/99-1/100的值将结果存入变量J中。

程序代码:
Private Sub Form_Click()
Dim I As Single
x = 0
For i = 1 To 100
x = (-1)^(i+1)/i
J = j + x
Next i
Print j
End Sub
程序改错
1 题目:求s=2!+4!+6!+8!,阶乘的计算用Function过程fact实现。

程序代码:
Private Sub Form_Click()
Dim i as integer, s as long
For i = 2 to 8 →For i = 2 to 8 step 2
s = s + fact(i)
Next i
Print s
End Sub
Public Function fact( ) →Public Function fact(Byval n%)
Dim t as long
Dim i as integer
t = 1
For i = 1 to n
t = t * i
Next i
Fact = i →Fact = t
End function
2 题目:请根据下列描述编写购物优惠程序。

某商场为了加速促成商品流通,采用购物打折的优惠办法,每位顾客一次购物(1)在100元以上者,按九五折优惠;(2)在200元以上者,按九折优惠;(3)在300元以上者,按八折优惠;(4)在500元以上者,按七折优惠。

在窗体上添加两个文本框和一个命令按钮,要求在Text1中输入购物商品总金额,单击命令按钮,在Text2中输出优惠价。

程序代码:
Private Sub Command1_Click ()
Dim x As Integer, y As Single
x = Val(Text1.Text)
If x < 100 then
x = y →y=x
Else
If x < 200 then
y = 0.95 * x
If x < 300 then
y = 0.9 * x
Else
If x < 300 then
y = 0.8 * x
Else
y = 0.7 * x
Else If →End If
End If
End If
End If
Text2.text = x →Text2.text = y
End Sub
1 题目:由输入对话框输入n(n必须为大于零且小于30的自然数),计算下列表达式的值,并在标签框label1上显示。

Sum=1/2+1/6+……+1/(k*(k+1)) (k<=n)
Private sub form_click()
Dim n as integer, sum as double, k as integer
N = val(inputbox(“n=”,”请输入自然数n(n>0且n<30)”))
Do while n <= 0 or n > 30
N = val(inputbox(“n=”,”请重新输入”))
Loop
Sum = 0
K = 0
Do
K = k+1
Sum = sum + 1/(k * (k+1))
Loop until (k+1) > n
Label1.caption = “sum” + str(sum)
End sub
2 题目:下面的程序用于打印出以下图形。

*
***
*****
*******
*****
***
*
程序代码:
Private sub form_click()
Dim i%, j%, k%
For I = 3 to 0 step -1
For j = 0 to 2 * i
Print “ ”;
Next j
For k = 5 to !! 11-2*i !!
Print “*” ;
Next k
print
Next i
For I = 0 to 3
For j = 0 to 2 * I + 2
print “ ”;
Next j
For k = 0 to 4-2 * i
Print “*” ;
Next k
print
Next i
End sub。

相关文档
最新文档