VB程序改错40题含答案

更新时间:2024-07-01 18:04:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

【程序改错】

题目:该程序实现将输入的 0 - 255 之间的正整数转换成二进制数 Option Explicit

Private 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

For m = 1 To n a(m) = x Mod 2 x = x / 2 Next m s = \

For m = n To 0 Step -1 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 Explicit Dim A As Integer Dim B As Integer Dim C As Integer

Private Sub Form_Click() Dim nTemp As Integer

A = Val(InputBox(\输入正整数\ B = Val(InputBox(\输入正整数\ C = Val(InputBox(\输入正整数\ 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 \End 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 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 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 答案:

=======(答案1)======= Print Spc(n);

=========或========= Print Spc(i);

=========或========= ? Spc(n);

=========或========= ? Spc(i);

=======(答案2)======= n = n - 1

=========或========= n = - 1+n

=======(答案3)======= m = m + 1

第5题 (1.0分) 题号:469 '【程序改错】

'题目:已知一个函数f(x)=1000*sin(x),利用绘图方法 ' 在图片框中显示其图形。结果如图1 Option Explicit

Private Const pi = 3.14159 Private Sub Command1_Click()

Dim x As Integer

Picture1.Scale (-pi, -1200)-(pi, 1200)

For x = -pi To pi Step pi

Picture1.PSet (x, 1000 * pi * Sin(x)), vbRed Next x End 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 Explicit

Private 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)

S1 = RTrim(S1)

For i = 1 To Len(S1)

Ch = Mid(N, i, 1)

Sum = Val(Ch) Next i

Print \该整数的各位数之和是:\End Sub 答案:

=======(答案1)======= S1 = Trim(S1)

=========或========= S1 = lTrim(S1)

=======(答案2)======= Ch = Mid(S1, i, 1) =======(答案3)======= Sum = Sum + Val(Ch) =========或========= Sum = Sum + Val(Ch)

第7题 (1.0分) 题号:454 '【程序改错】

'题目:本程序的功能是随机产生的10个两位正整数,并进行递减排序。 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 Next J

Print X(I); Next I Print End Sub

Private Sub Command1_Click() CreateRND End Sub 答案:

=======(答案1)=======

X(I) = Int(10 + Rnd() * 90) =========或=========

X(I) = Int(10 + Rnd() * 90) =======(答案2)======= If X(I) < X(J) Then =========或========= If X(I) <= X(J) Then =========或========= If X(J) > X(I) Then =========或========= If X(J) >= X(I) Then =======(答案3)======= temp = X(J)

第9题 (1.0分) 题号:130 '【程序改错】

'题目:以下程序段用于计算5的N次方。 Option Explicit

Private Sub Form_Click()

Dim n As Integer, k As Integer, s As Long n = InputBox(\ k = 0 s = 0

Do While k <= n s = s * 5 k = k + 1 Next

Print \的\次方是\End Sub 答案:

=======(答案1)======= k=1

=======(答案2)======= s=1

=======(答案3)=======

=======(答案3)======= N = N - 1

第20题 (1.0分) 题号:139 '【程序改错】

'题目:随机产生并输出100以内大于50的20个整数,输 ' 出时每5个数一行。 Option Explicit

Private Sub Form_Click() Randomize Timer

Dim i As Integer, ma As Integer i = 1

Do Until i < 20

ma = Rnd() * 100 \\ 1 If ma > 50 Then Print ma; i = i + 1 If i \\ 5 = 0 Then Print End If End If Loop End Sub 答案:

=======(答案1)======= i = 0

=======(答案2)======= Do while i < 20

=========或========= Do while 20 > i

=========或========= Do while i <=19

=========或========= Do while 19 > =i

=======(答案3)======= If i mod 5 = 0 Then

第23题 (1.0分) 题号:474 '【程序改错】

'题目:程序功能为求解一元二次方程的实根,请修正程序中错误。 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 Then

root1 = (Sqr(work)) / (2 * a)

root2 = (Sqr(work)) / (2 * a)

Debug.Print \有二个实根\ Else

Debug.Print \无实根!\ End If End Sub 答案:

=======(答案1)======= work = b ^ 2 - 4 * a * c =========或========= work = b *b - 4 * a * c =======(答案2)=======

root1 = (-b + Sqr(work)) / (2 * a) =========或=========

root1 = ( Sqr(work) -b ) / (2 * a) =========或=========

root1 = (-b + Sqr(work)) / ( a*2) =========或=========

root1 = ( Sqr(work) -b ) / ( a*2) =======(答案3)=======

root2 = (-b - Sqr(work)) / (2 * a) =========或=========

root2 = (- Sqr(work) - b) / (2 * a) =========或=========

root2 = (-b - Sqr(work)) / ( a*2) =========或=========

root2 = (- Sqr(work) - b) / (a*2) 第28题 (1.0分) 题号:133 '【程序改错】

'题目:下面程序将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 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 答案:

=======(答案1)======= For n = m + 1 To 10 =======(答案2)======= If a(t)

If a(n) >a(t) Then t = n =======(答案3)======= If t <> m Then =========或========= If not t = m Then =========或========= If not m = t Then

第30题 (1.0分) 题号:480 '【程序改错】

'题目:下面函数的功能是:求变量s(s=a+aa+aaa+aaaa+??) ' 的值。其中,a是一个0-9的数字,总共累加a项。 ' 例如,当a=3时,s=3+33+333 (共累加3项)。 Option Explicit

Public Function Calc(a As Integer) Dim s As Long Dim t As Long Dim i As Integer s = a

t = 1

For i = 2 To a

t = t + a s = s + t Next i Calc = s Print s End Function

Private Sub Command1_Click() Dim i As Integer

i = InputBox(\请输入数字(0-9):\

Calc call i End Sub 答案:

=======(答案1)======= t = a

=======(答案2)======= t = t * 10 + a

=========或========= t = t * 10 + a

=========或========= t = t * 10 + a

=======(答案3)======= Calc i

=========或========= Call Calc(i)

=========或========= Calc (i)

第31题 (1.0分) 题号:489

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:给定三角形的三条边,计算三角形的面积。要求 ' 程序首先判断给定的三条边能否构成三角形。 '------------------------------------------------ Option Explicit

Private Sub Form_Click()

Dim a As Single, b As Single, c As Single Dim s As Single, t As Single start:

a = InputBox(\输入1边长:\ b = InputBox(\输入2边长:\ c = InputBox(\输入3边长:\

If a + b < c Or b + c < a Then

MsgBox (\不能构成三角形,请重新输入个边\ GoTo start End If

t = (a + b) / 2

s = Sqr((t - a) * (t - b) * (t - c)) Print \该三角形的面积:\End Sub 答案:

=======(答案1)=======

If a + b < c Or b + c < a Or a + c < b Then =======(答案2)======= t = (a + b + c) / 2 =========或========= t = (a + b + c ) / 2 =======(答案3)=======

s = Sqr(t * (t - a) * (t - b) * (t - c)) =========或=========

s = Sqr( t * (t - a) * (t - b) * (t - c) )

第32题 (1.0分) 题号:475

'------------------------------------------------ '【程序改错】

'------------------------------------------------

'题目:本程序求3~100之间的所有素数(质数)并统计个数;

' 同时将这些素数从小到大依次写入顺序文件c:\\dataout.txt; ' 素数的个数显示在窗体Form1上。

'------------------------------------------------ Option Explicit

Private Sub Command1_Click()

Dim Count As Integer, Flag As Boolean Dim t1 As Integer, t2 As Integer

Open \ 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

If Flag = False Then Count = Count + 1

Write #1, t2 End If Next t1

Form1.Print \素数个数\ Close #1 End Sub 答案:

=======(答案1)=======

Open \=========或=========

Open \=======(答案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 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 答案:

=======(答案1)======= s =0

=======(答案2)======= For i = 7 To 832 step 30 =======(答案3)======= Next i

=========或========= Next

第35题 (1.0分) 题号:131

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:下面程序用于将十个数字从小到大排序

'------------------------------------------------ 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 答案:

=======(答案1)=======

For i = 1 To 10

=======(答案2)=======

If a(j) > a( j + 1 ) Then =========或=========

If a( j + 1 )

第40题 (1.0分) 题号:141

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:输出40以内能够被3整除的数,要求输出结果为5 ' 个数一行。

'------------------------------------------------ Option Explicit

Private Sub Form_Click() Cls

Dim x As Integer Dim i As Integer i = 1

For x = 1 To 40

If (x / 3) = (x \\ 3) Then Print x i = i + 1 End If

If i Mod 3 = 0 Then Print End If Next x End Sub 答案:

=======(答案1)======= i = 0

=======(答案2)======= Print x;

=======(答案3)======= if i mod 5=0 then

第42题 (1.0分) 题号:117

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:下面的程序段用于实现以下功能:建立一顺序文 ' 件,存放10名同学的学号和三门功课成绩,显示 ' 该文件内所有记录, 并同时显示其总分和平均分 '------------------------------------------------ Option Explicit

Private Sub Form_Click()

Dim no%, c1%, c2%, c3%, i As Integer

Open \For i = 1 To 10

no = InputBox(\请输入学号\ c1 = InputBox(\请输入数学成绩\ c2 = InputBox(\请输入语文成绩\ c3 = InputBox(\请输入外语\ Write #1, no, c1, c2, c2 Next i Close #1

Open \For i = 1 To 10

Print #1, no, c1, c2, c3

Print no, c1, c2, c3, c1 + c2 + c3, (c1 + c2 + c3) / 3 Next i Close #1 End Sub 答案:

=======(答案1)=======

Open \=======(答案2)=======

Open \=======(答案3)======= Input #1, n0, c1, c2, c3 第43题 (1.0分) 题号:118

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:以下程序段用于输出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 答案:

=======(答案1)======= swit=0

=======(答案2)=======

While i <= k And swit = 0 =========或=========

While k>=i And swit = 0 =======(答案3)======= i= i + 1

第45题 (1.0分) 题号:135

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:程序功能:求1+2+3??,直到其和超出3000为 ' 止,并输入结果。

'------------------------------------------------ Option Explicit

Private Sub Form_Click() Cls

Dim i As Integer Dim s As Single i = 1 s = 1 Do

i = i + 2 s = s + i Loop s > 3000

Print \从1 到:\的和是\End Sub 答案:

=======(答案1)======= s =0

=======(答案2)======= i = i + 1

=======(答案3)======= Loop until s > 3000 =========或========= Loop until 3000 < s

第46题 (1.0分) 题号:144

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:下面程序的作用是产生100以内的全部素数,并 ' 按每行5个数据输出。

'------------------------------------------------ Option Explicit

Private Function prime(ByVal n As Integer) Dim i As Integer prime = 1

If n <= 1 Then prime = 0

For i = 1 To n - 1

If n Mod i = 0 Then prime = 0 Next i End Function

Private Sub Form_Click()

Dim i As Integer, k As Integer k = 0

For i = 1 To 100

If prime(i) = 1 Then

Print Tab((k Mod 5) * 8); i k = k + 1

If k Mod 4 = 0 Then Print ; End If Next i End Sub 答案:

=======(答案1)======= For i = 2 To n - 1 =======(答案2)=======

Print Tab( (k Mod 5) * 8 ); i; =======(答案3)======= If k Mod 5 = 0 Then Print 第51题 (1.0分) 题号:482

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:请根据下列描述编写购物优惠程序。某商场为了 ' 加速促成商品流通,采用购物打折的优惠办法,每

' 位顾客一次购物(1)在100元以上者,按九五折优惠; ' (2)在200元以上者,按九折优惠;(3)300元以上 ' 者,按八折优惠;(4)500元以上者按七折优惠。 '------------------------------------------------ Option Explicit

Private Sub Command1_Click() Dim x As Single, y As Single x = Val(Text1.Text) If x < 100 Then

x = y Else

If x < 200 Then y = 0.95 * x Else

If x < 300 Then y = 0.9 * x Else

If x < 500 Then y = 0.8 * x Else

y = 0.7 * x

Else If End If End If End If

Text2.Text = x End Sub 答案:

=======(答案1)======= y = x

=======(答案2)======= End If

=======(答案3)======= Text2.Text = y

=========或========= Text2 = y

第52题 (1.0分) 题号:450

'------------------------------------------------ '【程序改错】

'------------------------------------------------

'题目:统计一顺序文件text.txt中的空格、字母、数字和其它字符个数。 '------------------------------------------------ Option Explicit

Private Sub Command1_Click() Dim s As String, C As String

Dim I As Integer, L As Integer, spac As Integer, character As Integer, digit As Integer, other As Integer

Open App.Path & \

Do Until EOF(0) Line Input #1, s L = Len(s) For I = 1 To L

C = Mid(s, I, 1)

If C >= \ character = character + 1 ElseIf C = \ spac = spac + 1

ElseIf C >= \ digit = digit + 1 Else

other = other + 1 End If Next I Loop

Close #1

Print \字符个数为:\数字个数为:\ Print \空格个数为:\其它个数为:\End Sub 答案:

=======(答案1)=======

Open App.Path & \=========或=========

Open \=======(答案2)======= Do Until EOF(1)

=========或========= Do While Not EOF(1) =======(答案3)=======

ElseIf C >= \第56题 (1.0分) 题号:138

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:输出40以内能够被3整除的数,要求输出结果为 ' 5个数一行。

'------------------------------------------------ Option Explicit

Private Sub Form_Click() Cls

Dim x As Integer Dim i As Integer i = 1

For x = 1 To 40

If (x / 3) = (x \\ 3) Then Print x i = i + 1 End If

If i Mod 5 = 0 Then Print End If step i End Sub 答案:

=======(答案1)======= i = 0

=======(答案2)======= Print x ;

=========或========= ? x ;

=======(答案3)======= Next x

=========或========= Next

第57题 (1.0分) 题号:465

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:用InputBox函数输入一个字符串,编写程序按与 ' 输入的字符相反的次序用Msgbox函数输出这个字 ' 符串。如输入字符串为\,则输出为\' gfedcba\,输出效果如图1。

'------------------------------------------------ Option Explicit

Private Sub Command1_Click()

Dim pristr As String, outstr As String Dim i As Integer

pristr = InputBox(\

For i = 0 To Len(pristr)

outstr = outstr + Mid(pristr, Len(pristr) - i) Next i

MsgBox outstr, , \End Sub 答案:

=======(答案1)=======

For i = 0 To Len(pristr) - 1 =========或=========

For i = 0 To -1+Len(pristr) =======(答案2)=======

outstr = outstr + Mid(pristr, Len(pristr) - i, 1) =========或=========

outstr = Mid(pristr, Len(pristr) - i, 1)+outstr =======(答案3)=======

MsgBox outstr, vbQuestion, \第59题 (1.0分) 题号:147

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:下面的程序用来产生并输出图示的杨辉三角。 ' 1 ' 1 1 ' 1 2 1 ' 1 3 3 1 ' 1 4 6 4 1 ' 1 5 10 10 5 1

'------------------------------------------------ Option Explicit

Public Sub readin(a() As Integer, n As Integer) Dim i, j As Integer For i = 1 To n

a(i, n) = 1 a(i, 1) = 1 Next i

For i = 3 To n For j = 2 To n

a(i, j) = a(i - 1, j) + a(i - 1, j - 1) Next j Next i End Sub

Public Sub printf(a() As Integer, n As Integer) Dim i, j As Integer For i = 1 To n For j = 1 To n

Print Tab(5 * j + 10); a(i, j); Next j Print Next i Print End Sub

Private Sub Form_Click() Dim x(10, 10) As Integer Call readin(x(), 6) Call printf(x(), 6) End Sub 答案:

=======(答案1)======= a(i, i) = 1

=======(答案2)======= For j = 2 To i - 1 =========或========= For j = 2 To -1 + i =======(答案3)======= For j = 1 To i

第60题 (1.0分) 题号:146

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:用辗转相除法求两个整数的最大公约数。

'------------------------------------------------ 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(\输入一个整数\ b = InputBox(\输入一个整数\ a = Val(a) b = Val(b) Call gcd(a, b) Print a, b, c End Sub 答案:

=======(答案1)======= m = n

=======(答案2)======= gcd = n

=======(答案3)======= c = gcd(a, b)

第65题 (1.0分) 题号:492

'------------------------------------------------ '【程序改错】

'------------------------------------------------ '题目:输入两个正整数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 Loop

Print \两数的最大公因子为:\End Sub 答案:

=======(答案1)======= r = m Mod n

=======(答案2)======= n = r

=======(答案3)=======

Print \两数的最大公因子为:\第66题 (1.0分) 题号:467

'------------------------------------------------ '【程序改错】

'------------------------------------------------

Next c Next b Next a End Sub

5、判断某年是否闰年

Private Sub Command1_Click() Dim N As Integer

N = InputBox(\输入年号:\判断闰年\ If N / 100 = N \\ 100 Then

If N Mod 400 = 0 Then Print \闰年\不是闰年\ Else

If N Mod 4 = 0 Then Print \闰年\不是闰年\ End If End Sub

本文来源:https://www.bwwdw.com/article/qc9.html

Top