《VB程序改错40题(58页).doc》由会员分享,可在线阅读,更多相关《VB程序改错40题(58页).doc(58页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-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 =
2、 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(
3、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 En
4、d If Print The integers in order is; A; B; CEnd Sub答案:=(答案1)=If A = A Then=(答案2)=If A = A Then=(答案3)=If B = 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
5、* 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 Pi
6、cture1.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.
7、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 =
8、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
9、= 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答案:=(答
10、案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次方
11、是; 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
12、 a integer value:) x = Val(InputBox(Please input a single value:) If n 1 Or x 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=
13、(答案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()
14、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
15、) 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
16、 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 Int
17、eger, 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 (没有此元
18、素): 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
19、_Click()Randomize TimerDim i As Integer, ma As Integer*FOUND*i = 1*FOUND*Do Until i 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 = 0 And a 0 Then *FOUND* root1 = (Sqr(work) / (2 * a) *FOUND* root2 = (Sqr(work) / (2 * a) Debug.Print
20、有二个实根 + 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 Inte
21、gerFor 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
22、(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
23、 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 Sing
24、le, 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答案:=
25、(答案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 t
26、1 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
27、答案:=(答案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