VB程序改错40题(58页).doc
-VB程序改错40题-第 58 页第2题 (1.0分) 题号:129'【程序改错】'题目:该程序实现将输入的 0 - 255 之间的正整数转' 换成二进制数Option ExplicitPrivate Sub Form_Click() Const n = 8 Dim a(n) As Integer, s As String, m As Integer, x As Integer x = Val(InputBox("请输入一个 0 - 255 之间的正整数:") Print x'*FOUND* For m = 1 To n a(m) = x Mod 2'*FOUND* x = x / 2 Next m s = " " For m = n To 0 Step -1'*FOUND* s = Str(a(m) Next m Print s End Sub答案:(答案1)For m = 0 To n=(答案2)x= x 2或x= int(x/2)=(答案3)s = s + Str(a(m)第3题 (1.0分) 题号:463'题目:以下程序功能是输入三个数,由大到小排序。Option ExplicitDim A As IntegerDim B As IntegerDim C As IntegerPrivate Sub Form_Click() 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", "输入正整数") '*FOUND* If A <= C Then nTemp = A A = B B = nTemp End If '*FOUND* If B <= C Then nTemp = A A = C C = nTemp End If '*FOUND* If A <= B Then nTemp = B B = C C = nTemp End If Print "The integers in order is" A; B; CEnd Sub答案:=(答案1)=If A <= B Then=或=If B >= A Then=(答案2)=If A <= C Then=或=If C >= A Then=(答案3)=If B <= C Then=或=If C >= B Then第4题 (1.0分) 题号:127'题目:下面程序可输出如下图形:Option ExplicitPrivate Sub Form_Click()Dim m As Integer, n As Integer, s As String, i As Integer, j As Integern = 4m = 1s = "*"For i = 5 To 1 Step -1 '*FOUND* Print Spc(n) For j = 1 To 2 * m - 1 Print s; Next j Print '*FOUND* n = n + 1 '*FOUND* m = m - 1Next iEnd Sub答案:=(答案1)=Print Spc(n);=(答案2)=n = n - 1=(答案3)=m = m + 1第5题 (1.0分) 题号:469'题目:已知一个函数f(x)=1000*sin(x),利用绘图方法' 在图片框中显示其图形。结果如图1Option ExplicitPrivate Sub Command1_Click() '*FOUND* Dim x As Integer Picture1.Scale (-pi, -1200)-(pi, 1200) '*FOUND* For x = -pi To pi Step pi '*FOUND* Picture1.PSet (x, 1000 * pi * Sin(x), vbRed Next xEnd Sub答案:=(答案1)=Dim x As Single=或=Dim x!=(答案2)=For x = -pi To pi Step pi / 180=(答案3)=Picture1.PSet (x, 1000 * Sin(x), vbRed=或=Picture1.PSet (x, Sin(x)*1000), vbRed第6题 (1.0分) 题号:497'题目:编程求一个十进制整数n的各位数字之和,设n为小于或等于5位的数Option ExplicitPrivate Sub Form_Click() Dim N As Integer, Sum As Integer, S1 As String, S2 As String Dim i As Integer, Ch As String Sum = 0 N = InputBox("输入整数n") S1 = Str(N) '*FOUND* S1 = RTrim(S1) For i = 1 To Len(S1) '*FOUND* Ch = Mid(N, i, 1) '*FOUND* Sum = Val(Ch) Next i Print "该整数的各位数之和是:" SumEnd Sub答案:=(答案1)=S1 = Trim(S1)=(答案2)=Ch = Mid(S1, i, 1)=(答案3)=Sum = Sum + Val(Ch)第7题 (1.0分) 题号:454'题目:本程序的功能是随机产生的10个两位正整数,并进行递减排序。Option ExplicitPrivate 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 '*FOUND* X(I) = Int(Rnd() * 90) Print X(I); Next I Print Print "排序:" For I = 0 To N - 1 For J = I + 1 To N '*FOUND* If X(I) > X(J) Then '*FOUND* Temp = X(I) X(J) = X(I) X(I) = Temp End If Next J Print X(I); Next I PrintEnd SubPrivate Sub Command1_Click() CreateRNDEnd Sub答案:=(答案1)=X(I) = Int(10 + Rnd() * 90)=(答案2)=If X(I) < X(J) Then=(答案3)=temp = X(J)第9题 (1.0分) 题号:130'题目:以下程序段用于计算5的N次方。Option ExplicitPrivate Sub Form_Click() Dim n As Integer, k As Integer, s As Long n = InputBox(" Input n ")'*FOUND* k = 0'*FOUND* s = 0 Do While k <= n s = s * 5 k = k + 1'*FOUND* Next Print "5的" "n次方是" sEnd Sub答案:=(答案1)=k=1=(答案2)=s=1=(答案3)=Loop第11题 (1.0分) 题号:452'题目:用自定义函数的方法求sum(x),求当-1x1时,' sum(x)=x/2!+x2/3!+x3/4!+xn/(n+1)!,' 当x1或x-1时,函数值为0。当n=0时,输入数' 据错误。X、N都是由用户输入。Option ExplicitPrivate 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 + vbExclamation, "数据输入") Exit Sub End If s = Sum(x, n) Print sEnd SubFunction 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 '*FOUND* Exit Do Else '*FOUND* For i = 2 To n ss = ss * i '*FOUND* Sum = x (i - 1) / ss Next i End IfEnd Function答案:=(答案1)=exit Function=(答案2)=For i = 2 To n + 1=或=For i = 2 To 1+n=(答案3)=sum = sum + x (i - 1) / ss第14题 (1.0分) 题号:136'题目:程序功能为打印下列图形:Option ExplicitPrivate Sub Form_Click()ClsDim i As IntegerDim j As Integer'*FOUND*For i = 1 To 7'*FOUND* For j = 1 To 5 Print "*"'*FOUND* LoopPrintNext iEnd Sub答案:=(答案1)=For i = 1 To 5=(答案2)=For j = 1 To i=(答案3)=Next j=或=Next第16题 (1.0分) 题号:461'题目:产生30个小于100的成绩随机数,并统计出优、良' 、中等、及格、不及格数的个数,并计算出成绩属' 于优秀段的成绩平均分。Option ExplicitPrivate Sub Form_Click() Dim k%, a%, bjg%, jg%, zd%, lh%, yx As Integer Dim pjf As Integer Randomize pjf = 0 For k = 1 To 30 '*FOUND* a = Int(Rnd() Select Case a Case 0 To 59 bjg = bjg + 1 '不及格 Case 60 To 69 jg = jg + 1 '及格 Case 70 To 79 zd = zd + 1 '中等 Case 80 To 89 lh = lh + 1 '良好 Case 90 To 100 yx = yx + 1 '优秀 '*FOUND* pjf = pjf + 1 End Select Next k '*FOUND* If yx > 0 Then pjf = pjf / 30 Debug.Print "不及格" + Str$(bjg) + "人,及格" + Str$(jg) + "人,中等" + Str$(zd) + "人" Debug.Print "良好" + Str$(lh) + "优秀" + Str$(yx) + "人" Debug.Print "优秀分数段成绩平均分" & pjfEnd Sub答案:=(答案1)=a = Int( Rnd * 100)=(答案2)=pjf = pjf + a=(答案3)=If yx > 0 Then pjf = pjf / yx第17题 (1.0分) 题号:456'题目:挑选单数并排序程序:程序启动后由计算机自动产' 生20个属于100,300之间的随机整数,单击"显' 示全体"按钮时,在Form1上显示这20个随机数;' 单击"显示奇数"按钮时,在Form1上显示其中的奇数;' 单击"排序"按钮时,在Form1上将这些奇数从小到大显示。' 要求显示格式为每行显示5个数据。Option ExplicitPrivate a(20) As Integer, b(20) As IntegerPrivate k As IntegerPrivate Sub cmdodd_Click()Dim I As Integerk = 0For I = 1 To 20 '*FOUND* If a(I) / 2 = Int(a(I) / 2) Then k = k + 1 b(k) = a(I) End IfNext IFor I = 1 To k Print b(I); '*FOUND* If Int(I / 5) <> I / 5 Then PrintNext IPrintEnd SubPrivate Sub cmdAll_Click() Randomize Dim I As Integer For I = 1 To 20 '*FOUND* a(I) = Int(Rnd() * 20 + 100) Print a(I), If Int(I / 5) = I / 5 Then Print Next I PrintEnd SubPrivate Sub cmdsort_Click() Dim I As Integer Dim J As Integer Dim Temp As Integer For I = 1 To k - 1 For J = I To k If b(I) > b(J) Then Temp = b(I): b(I) = b(J): b(J) = Temp Next J Next I For I = 1 To k Print b(I); If Int(I / 5) = I / 5 Then Print Next IEnd Sub答案:=(答案1)=If a(I) mod 2 <>0 Then=(答案2)=If I mod 5 = 0 Then Print=(答案3)=a(I) = Int(Rnd() * 200 + 100)第19题 (1.0分) 题号:499'题目:编程将一个数从已经有序(设从小到大排序)的数组' 中删除,使数组还继续保持有序而且其余元素按照下' 标连续存放。Option ExplicitPrivate Sub Form_Click() Dim A() As Integer, N As Integer, Flag As Integer Dim i As Integer, X As Integer, P As Integer Flag = 0 N = InputBox("输入元素个数") '*FOUND* Dim A(N) For i = 1 To N A(i) = InputBox("输入数组的第" & i & "个元素") Next i Print "删除某元素前的数组" For i = 1 To N Print A(i); Next iStart: X = InputBox("输入要删除的元素:") For i = 1 To N If X = A(i) Then P = i: Flag = 1 Next i If Flag = 0 Then MsgBox ("没有此元素"): GoTo Start For i = P To N - 1 '*FOUND* A(i + 1) = A(i) Next i '*FOUND* N = N + 1 Print "删除元素后的数组:" For i = 1 To N Print A(i); Next iEnd Sub答案:=(答案1)=ReDim A(N)=(答案2)=A(i) = A(i + 1)=或=A(i) = A(1 + i)=(答案3)=N = N - 1第20题 (1.0分) 题号:139'题目:随机产生并输出100以内大于50的20个整数,输出时每5个数一行。Option ExplicitPrivate Sub Form_Click()Randomize TimerDim i As Integer, ma As Integer'*FOUND*i = 1'*FOUND*Do Until i < 20 ma = Rnd() * 100 1 If ma > 50 Then Print ma; i = i + 1'*FOUND* If i 5 = 0 Then Print End IfEnd IfLoopEnd Sub答案:=(答案1)=i = 0=(答案2)=Do while i < 20=(答案3)=If i mod 5 = 0 Then第23题 (1.0分) 题号:474'题目:程序功能为求解一元二次方程的实根,请修正程序中错误。Option ExplicitPrivate Sub Form_Load() Dim a!, b!, c!, root1#, root2#, work As Double a = Val(InputBox(" 请输入系数a的值") b = Val(InputBox(" 请输入系数b的值") c = Val(InputBox(" 请输入系数c的值") '*FOUND* work = b * 2 - 4 * a * c If work >= 0 And a <> 0 Then '*FOUND* root1 = (Sqr(work) / (2 * a) '*FOUND* root2 = (Sqr(work) / (2 * a) Debug.Print "有二个实根" + Str$(root1) + "," + Str$(root2) Else Debug.Print "无实根!" End IfEnd Sub答案:=(答案1)=work = b *b - 4 * a * c=(答案2)=root1 = ( Sqr(work) -b ) / ( a*2)=(答案3)=root2 = (-b - Sqr(work) / (2 * a)=或=第28题 (1.0分) 题号:133'题目:下面程序将10个整数从大到小排序Option ExplicitPrivate Sub Form_Click()Dim t%, m%, n%, w%Dim a(10) As IntegerFor m = 1 To 10 a(m) = Int(10 + Rnd() * 90) Print a(m); " "Next mPrintFor m = 1 To 9 t = m'*FOUND* For n = 2 To 10'*FOUND* If a(t) > a(n) Then n = t Next n'*FOUND* If t = m Then w = a(m) a(m) = a(t) a(t) = w End IfNext mFor m = 1 To 10 Print a(m)Next mEnd Sub答案:=(答案1)=For n = m + 1 To 10=(答案2)=If a(t) <a(n) Then t = n=(答案3)=If t <> m Then第30题 (1.0分) 题号:480'题目:下面函数的功能是:求变量s(s=a+aa+aaa+aaaa+)' 的值。其中,a是一个0-9的数字,总共累加a项。' 例如,当a=3时,s=3+33+333 (共累加3项)。Option ExplicitPublic Function Calc(a As Integer) Dim s As Long Dim t As Long Dim i As Integer s = a '*FOUND* t = 1 For i = 2 To a '*FOUND* t = t + a s = s + t Next i Calc = s Print sEnd FunctionPrivate Sub Command1_Click() Dim i As Integer i = InputBox("请输入数字(0-9):") '*FOUND* Calc call iEnd Sub答案:=(答案1)=t = a=(答案2)=t = t * 10 + a=(答案3)=Call Calc(i)第31题 (1.0分) 题号:489'题目:给定三角形的三条边,计算三角形的面积。要求' 程序首先判断给定的三条边能否构成三角形。Option ExplicitPrivate Sub Form_Click() Dim a As Single, b As Single, c As Single Dim s As Single, t As Singlestart: a = InputBox("输入1边长:") b = InputBox("输入2边长:") c = InputBox("输入3边长:") '*FOUND* If a + b < c Or b + c < a Then MsgBox ("不能构成三角形,请重新输入个边") GoTo start End If '*FOUND* t = (a + b) / 2 '*FOUND* s = Sqr(t - a) * (t - b) * (t - c) Print "该三角形的面积:" sEnd Sub答案:=(答案1)=If a + b < c Or b + c < a Or a + c < b Then=(答案2)=t = (a + b + c) / 2=(答案3)=s = Sqr(t * (t - a) * (t - b) * (t - c)第32题 (1.0分) 题号:475'题目:本程序求3100之间的所有素数(质数)并统计个数;' 同时将这些素数从小到大依次写入顺序文件c:dataout.txt;' 素数的个数显示在窗体Form1上。Option ExplicitPrivate Sub Command1_Click() Dim Count As Integer, Flag As Boolean Dim t1 As Integer, t2 As Integer '*FOUND* Open "dataout.txt" For Input As #1 Count = 0 For t1 = 3 To 100 Flag = True For t2 = 2 To Int(Sqr(t1) If t1 Mod t2 = 0 Then Flag = False Next t2 '*FOUND* If Flag = False Then Count = Count + 1 '*FOUND* Write #1, t2 End If Next t1 Form1.Print "素数个数" Count Close #1End Sub答案:=(答案1)=Open "dataout.txt" For Output As #1=或=Open "dataout.txt" For Output As 1=(答案2)=If Flag Then=或=If Flag=true Then=(答案3)=Write #1, t1=或=Write 1, t1第33题 (1.0分) 题号:137'题目:求s=72+102+132+832的值。Option ExplicitPrivate Sub Form_Click()ClsDim s As LongDim i As Integer'*FOUND*s = 1'*FOUND*For i = 7 To 832s = s + i'*FOUND*loop 30Print sEnd Sub答案:=(答案1)=s =0=(答案2)=For i = 7 To 832 step 30=(答案3)=Next i=或=Next第35题 (1.0分) 题号:131