VB专题复习--程序改错题
【思考】
1、同学们在程序改错题中都遇到过哪些类型的错误?试举例
2、同学们在做程序改错中需要注意什么?
3、同学们在做程序改错题时存在哪些困难?
【练习】
【2012年高考题】下面的程序是将一输入的字符串进行分类赋值,方法:从字符串的最左端开始截取一字符,然后进行判断,数字字符放入数组A中,将其他字符放入数组b中,最后将两个数组中的值分类打印,程序中有四处错误,试改正。 Dim A(100) as integer Dim b(100) as integer Dim c as integer Dim i%,j%,k%,m%, w%
C=inputbox(“c=”,””,””) K=len(c) For i=1 to k W=right(c,1) C=right(c,k-1)
If w>=”0” and w<=”9” then A(j)=w:j=j+1 Else then
B(m)=w:m=m+1 End if K=k+1 Next i Print
For i=0 to j-1 Print a(i); Next i Print
For i=0 to m-1 Print b(i); Next i Print
【程序改错题纠错方法】 法一:功能实现法 法二:举例假设法
【2011年高考题】窗体上有一定时器timer1,有一命令按钮command1,一文本框text1。命令按钮的作用是启动定时器工作,使文本框中字号从6开始,每1秒钟增大6,运行效果如图。程序中有两处错误,每个事件中各有一个,请改正。
Private Sub Command1_Click() Timer1.Interval = 2000 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() Dim a As Integer
1
a = a + 1
Text1.FontSize = 6 * a
If Text1.FontSize > 40 Then a = 0 End Sub
法三:程序分解法
【等级考试题】挑选奇数并进行排序:程序启动后由计算机自动产生20个属于[100,300]之间的随机整数,单击“显示全体”按钮时,在form1上显示这20个随机数;单击“显示奇数”按钮时,在form1显示其中的奇数;单击“排序”按钮时,在form1上将这写奇数从小到大显示。要求显示格式为每行显示5个数据。
注:该题有5处错
Private a(20) As Integer, b(20) As Integer Private k As Integer
Private Sub CmdAll_Click() Randomize For i = 1 To 20
a(i) = Int(Rnd() * 20 + 100) Print a(i);
If Int(i / 5) = i / 5 Then Print End If Next i
Print End Sub
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 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 End If Next j Next i
For i = 1 To k
Print b(i);
If Int(i / 5) <> i / 5 Then Print Next i End Sub
【巩固提升】
【2013年高考题】窗体中(如图所示)有一个定时器控件(名称Timer1),有三个标签(Caption属性分别是滚动条、组合框、列表框),一个滚动条(名称HS1,Min和Max的属性初值分别为0和5,Value初值为0),一个组合框(名称Combo1),一个列表框(名称List1),组合框和列表框初始为空,一个框架,框架中有两个复选框(名称分别为Check1和Check2,Caption属性如图所示)。
程序的功能是:窗体启动后,定时器开始工作,工作时每隔3秒钟触发一次事件,Timer事件中完成4项工作:(1)滚动条的值加1;(2)往组合框中添加滚动条的Value值;(3)调用Combo1_Change过程;(4)当滚动条的值为5时定时器停止工作。在Combo1_Change过程中,要将滚动条当前值加上复选框中当时所有选中项的标题添加到列表框中,Combo1_Change过程中只给出了部分程序,省略的程序都是正确的。
程序中有两处错误, 请将错误行和相应的正确行写在答题纸横线上。
2
Private Sub Combo1_Change() List1.AddItem HS1.Value If Check1.Value = True Then
List1.List(HS1.Value - 1) = List1.List(HS1.Value - 1) + Check1.Caption End If ????
End Sub
Private Sub Timer1_Timer() HS1.Value = HS1.Value + 1 Combo1.AddItem HS1.Value Combo1_Change
If HS1.Value = 5 Then Timer1.Visible = False End Sub
相关推荐: