The first step is to de clare the ar ray. Suppose we have five questions. With -
out an array we would do the following to de clare our five variables:
Dim q1Answered As Boolean
Dim q2Answered As Boolean
Dim q3Answered As Boolean
Dim q4Answered As Boolean
Dim q5Answered As Boolean
If
we were to use an array, we would have one line:
Dim qAnswered(5) As Boolean
This will give us an array that con tains six boxes, numbered 0 through 5:
qAnswered(0)
,
qAnswered(1)
,
qAnswered(2)
,
qAnswered(3)
,
qAnswered(4)
, and
qAnswered(5)
. Note that we really only need five
boxes
in our ex ample, and we got six. There are many ways to avoid getting the
extra box, but un less you are an aspiring pro grammer,
the easiest thing to do is
simply ig nore box number 0.
Now, we can shorten our
Ini
tial
ize
pro cedure. It won’t be shorter with
two questions (or sig nificantly shorter with five),
but when you create some-
thing with ten or twenty questions it will be much shorter:
Sub Ini
tial
ize()
Dim i As Long
numCorrect = 0
numIncorrect = 0
For i = 1 to 5
qAnswered(i) = False
Next i
Do'stlaringiz bilan baham: