资源描述
【程序改错】
题目:该程序实现将输入的 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("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 integers in order is"; A; B; C
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 "该整数的各位数之和是:"; Sum
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(" Input n ")
k = 0
s = 0
Do While k <= n
s = s * 5
k = k + 1
Next
Print "5的"; "n次方是"; s
End Sub
答案:
=======(答案1)=======
k=1
=======(答案2)=======
s=1
=======(答案3)=======
Loop
第11题 (1.0分) 题号:452
'【程序改错】
'题目:用自定义函数的方法求sum(x),求当-1≤x≤1时,
' sum(x)=x/2!+x^2/3!+x^3/4!+……+x^n/(n+1)!,
' 当x〉1或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 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 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 Do
Else
For i = 2 To n
ss = ss * i
Sum = x ^ (i - 1) / ss
Next i
End If
End 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 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
答案:
=======(答案1)=======
For i = 1 To 5
=======(答案2)=======
For j = 1 To i
=======(答案3)=======
Next j
=========或=========
Next
第16题 (1.0分) 题号:461
'【程序改错】
'题目:产生30个小于100的成绩随机数,并统计出优、良
' 、中等、及格、不及格数的个数,并计算出成绩属
' 于优秀段的成绩平均分。
Option Explicit
Private 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
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 '优秀
pjf = pjf + 1
End Select
Next k
If yx > 0 Then pjf = pjf / 30
Debug.Print "不及格" + Str$(bjg) + "人,及格" + Str$(jg) + "人,中等" + Str$(zd) + "人";
Debug.Print "良好" + Str$(lh) + "优秀" + Str$(yx) + "人"
Debug.Print "优秀分数段成绩平均分" & pjf
End Sub
答案:
=======(答案1)=======
a = Int( Rnd() * 100)
=========或=========
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 Explicit
Private a(20) As Integer, b(20) As Integer
Private k As Integer
Private Sub cmdodd_Click()
Dim I As Integer
k = 0
For I = 1 To 20
If a(I) / 2 = Int(a(I) / 2) Then
k = k + 1
b(k) = a(I)
End If
Next I
For I = 1 To k
Print b(I);
If Int(I / 5) <> I / 5 Then Print
Next I
Print
End Sub
Private Sub cmdAll_Click()
Randomize
Dim I As Integer
For I = 1 To 20
a(I) = Int(Rnd() * 20 + 100)
Print a(I),
If Int(I / 5) = I / 5 Then Print
Next I
Print
End Sub
Private 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 I
End Sub
答案:
=======(答案1)=======
If a(I) / 2 <> Int(a(I) / 2) Then
=========或=========
If a(I) mod 2 <>0 Then
=========或=========
If 0<>a(I) mod 2 Then
=========或=========
If a(I) / 2 <> a(I)\ 2 Then
=========或=========
If int(a(I) / 2) <> a(I) / 2 Then
=========或=========
If a(I) \ 2 <> a(I)/2 Then
=======(答案2)=======
If Int(I / 5) = I / 5 Then Print
=========或=========
If I mod 5 = 0 Then Print
=========或=========
If 0 = I mod 5 Then Print
=========或=========
If I / 5= int(I / 5) Then Print
=========或=========
If I / 5= I\ 5 Then Print
=========或=========
If I \5= I/ 5 Then Print
=======(答案3)=======
a(I) = Int(Rnd() * 200 + 100)
第19题 (1.0分) 题号:499
'【程序改错】
'题目:编程将一个数从已经有序(设从小到大排序)的数组
' 中删除,使数组还继续保持有序而且其余元素按照下
' 标连续存放。
Option Explicit
Private 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("输入元素个数")
Dim A(N)
For i = 1 To N
A(i) = InputBox("输入数组的第" & i & "个元素")
Next i
Print "删除某元素前的数组"
For i = 1 To N
Print A(i);
Next i
Start:
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
A(i + 1) = A(i)
Next i
N = N + 1
Print "删除元素后的数组:"
For i = 1 To N
Print A(i);
Next i
End 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 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 "有二个实根" + Str$(root1) + "," + Str$(root2)
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) <a(n) Then t = n
=========或=========
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 "该三角形的面积:"; s
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 "dataout.txt" For Input As #1
Count = 0
展开阅读全文