《VB程序改错题-.pdf》由会员分享,可在线阅读,更多相关《VB程序改错题-.pdf(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、程序改错1.题目:已知C 的公式表示如下:n!C=-(n-m)!*m!函数过程fac()求 k!函数,在 Fomr_Click事件中完成运算,请修正程序中错误。-Option Explicit Private Function fac(k As Integer)As Single Dim x As Single,j As Integer x=0 For j=1 To k x=x j Next j fac=x End Function Private Sub Form_Click()Dim C As Single Dim m As Integer,n As Integer m=Val(Input
2、Box(请输入一个整数(m):)n=Val(InputBox(请输入一个整数(n):)C=fac(n)/fac(n-m)*fac(m)Print C=;C End Sub 2.题目:程序功能为打印下列图形:*-Option Explicit Private Sub Form_Click()Cls Dim i As Integer Dim j As Integer For i=1 To 7 For j=1 To 5 Print*;Loop Print Next i End Sub 3.题目:本程序的功能是随机产生的10 个两位正整数,并进行递减排序。-Option Explicit Privat
3、e Sub CreateRND()Dim Temp As Integer Dim I As Integer Dim N As Integer Dim X(10)As Integer Dim J As Integer N=10 Print 数据:For I=1 To N X(I)=Int(Rnd()*90)Print X(I);Next I Print Print 排序:For I=0 To N-1 For J=I+1 To N If X(I)X(J)Then Temp=X(I)X(J)=X(I)X(I)=Temp End If Next J Print X(I);Next I Print En
4、d Sub Private Sub Command1_Click()CreateRND End Sub 4.题目:随机产生并输出100 以内大于50 的 20 个整数,输出时每 5 个数一行。-Option Explicit Private Sub Form_Click()Randomize Timer Dim i As Integer,ma As Integer i=1 Do Until i 50 Then Print ma;i=i+1 If i 5=0 Then Print End If End If Loop End Sub 5 题目:以下程序段用于计算5 的 N 次方。-Option
5、Explicit Private Sub Form_Click()Dim n As Integer,k As Integer,s As Long n=InputBox(Input n)k=0 s=0 Do While k=n s=s*5 k=k+1 Next Print 5的;n次方是;s End Sub 6.题目:下面程序可输出如下图形:*-Option Explicit Private Sub Form_Click()Dim m As Integer,n As Integer,s As String,i As Integer,j As Integer n=4 m=1 s=*For i=5
6、To 1 Step-1 Print Spc(n)For j=1 To 2*m-1 Print s;Next j Print n=n+1 m=m-1 Next i End Sub 7.题目:用自定义函数的方法求sum(x),求当-1x1 时,sum(x)=x/2!+x2/3!+x3/4!+xn/(n+1)!,当 x1 或 x-1 时,函数值为0。当 n=0 时,输入数 据错误。X、N都是由用户输入。-Option Explicit Private Sub Command1_Click()Dim s As Single Dim n As Integer,x As Single,k As Integ
7、er n=Val(InputBox(Please input a integer value:)x=Val(InputBox(Please input a single value:)If n 1 Or x -1 Then Exit Do Else For i=2 To n ss=ss*i Sum=x (i-1)/ss Next i End If End Function 8.题目:以下程序功能是输入三个数,由大到小排序。-Option Explicit Dim A As Integer Dim B As Integer Dim C As Integer Private Sub Form_Cl
8、ick()Dim nTemp As Integer A=Val(InputBox(Please input first integer,输入正整数)B=Val(InputBox(Please input second integer,输入正整数)C=Val(InputBox(Please input third integer,输入正整数)If A=C Then nTemp=A A=B B=nTemp End If If B=C Then nTemp=A A=C C=nTemp End If If A=B Then nTemp=B B=C C=nTemp End If Print The in
9、tegers in order is;A;B;C End Sub 9.题目:用辗转相除法求两个整数的最大公约数。-Option Explicit Function gcd(ByVal m As Integer,ByVal n As Integer)As Integer Dim r As Integer r=m Mod n Do While r 0 n=m n=r r=m Mod n Loop gcd=r End Function Private Sub Form_Click()Dim a As Integer,b As Integer,c As Integer a=InputBox(输入一个整
10、数)b=InputBox(输入一个整数)a=Val(a)b=Val(b)Call gcd(a,b)Print a,b,c End Sub 10.题目:下面的程序用“冒泡”法完成数组a中的 10 个整数按升序排列,请修正程序中错误。-Option Explicit Private Sub Command1_Click()Dim a Dim i As Integer,j As Integer,a1 As Integer a=Array(-2,5,24,58,43,-10,87,75,27,83)For i=1 To 9 For j=i+1 To 9 If a(j)=a(i)Then a1=a(i
11、a(i)=a(j)a(j)=a(i)End If Next j Next i For i=0 To 9 Print a(i)Next i End Sub 11.题目:程序功能为求解一元二次方程的实根,请修正程序中错误。-Option Explicit Private Sub Form_Load()Dim a!,b!,c!,root1#,root2#,work As Double a=Val(InputBox(请输入系数a 的值)b=Val(InputBox(请输入系数b 的值)c=Val(InputBox(请输入系数c 的值)work=b*2-4*a*c If work=0 And a 0 T
12、hen root1=(Sqr(work)/(2*a)root2=(Sqr(work)/(2*a)Debug.Print 有二个实根 +Str$(root1)+,+Str$(root2)Else Debug.Print 无实根!End If End Sub 12.题目:下面程序的作用是产生100 以内的全部素数,并按每行 5 个数据输出。-Option Explicit Private Function prime(ByVal n As Integer)Dim i As Integer prime=1 If n 3000 Print 从 1 到:;i;的和是;s End Sub 15.题目:以下
13、程序段用于输出100-300 的所有素数-Option Explicit Private Sub Form_Click()Dim n As Integer,k As Integer,i As Integer,swit As Integer For n=101 To 300 Step 2 k=Int(Sqr(n)i=2 swit=1 While swit=0 If n Mod i=0 Then swit=1 Else i=i-1 End If Wend If swit=0 Then Print n;End If Next n End Sub 16.题目:本程序的功能是随机产生的10 个两位正整数
14、,并进行递减排序。-Option Explicit Private Sub CreateRND()Dim Temp As Integer Dim I As Integer Dim N As Integer Dim X(10)As Integer Dim J As Integer N=10 Print 数据:For I=1 To N X(I)=Int(Rnd()*90)Print X(I);Next I Print Print 排序:For I=0 To N-1 For J=I+1 To N If X(I)X(J)Then Temp=X(I)X(J)=X(I)X(I)=Temp End If N
15、ext J Print X(I);Next I Print End Sub Private Sub Command1_Click()CreateRND End Sub 17.题目:输入两个正整数m 和 n,求其最大公约数。-Option Explicit Private Sub Form_Click()Dim m As Integer,n As Integer,r As Integer m=InputBox(输入 m的值:)n=InputBox(输入 n 的值:)Do While n 0 r=m/n m=n n=m r Loop Print 两数的最大公因子为:;n End Sub 18题目:
16、下面程序将10 个整数从大到小排序-Option Explicit Private Sub Form_Click()Dim t%,m%,n%,w%Dim a(10)As Integer For m=1 To 10 a(m)=Int(10+Rnd()*90)Print a(m);Next m Print For m=1 To 9 t=m For n=2 n+1 To 10 If a(t)a(n)Then n=t Next n If t=m Then w=a(m)a(m)=a(t)a(t)=w End If Next m For m=1 To 10 Print a(m)Next m End Sub
17、 19.题目:求 s=72+102+132+832 的值。-Option Explicit Private Sub Form_Click()Cls Dim s As Long Dim i As Integer s=1 For i=7 To 832 s=s+i loop 30 Print s End Sub 20.题目:下面程序用于将十个数字从小到大排序-Option Explicit Option Base 1 Private Sub Form_Click()Cls Dim a(10)As Integer Dim i As Integer,j As Integer,temp As Integer For i=0 To 10 If i Mod 2=0 Then a(i)=i Else a(i)=-i Print a(i);Next Print For i=1 To 10 For j=1 To 10-i If a(j)a(j+1)Then temp=a(j):a(j)=a(j+1):a(j+1)=temp End If Next i,j For i=1 To 10 Print a(i);Next End Sub
限制150内