Powerful PowerPoint for Educators: Using Visual Basic for Applications to Make PowerPoint Interactive


Download 1.37 Mb.
Pdf ko'rish
bet150/191
Sana08.05.2023
Hajmi1.37 Mb.
#1442581
1   ...   146   147   148   149   150   151   152   153   ...   191
Bog'liq
2.2. Powerful PowerPoint For Educators

Int((100 – 1 + 1) * Rnd +
1)
or 
Int(100 * Rnd + 1)
. If we wanted numbers from 50 to 100, we would
have
Int((100 – 50 + 1) * Rnd + 50)
or 
Int(51 * Rnd + 50)
. Don’t
worry if you don’t quite un derstand the math; just use the simple formula, and
you will be fine.
Ran domly Gen er ated Ques tions
Let’s use ran dom numbers with a simple example. In this example, we will
want to ran domly generate one-digit ad dition prob lems. We will have a ti tle card
with a but ton linked to 
GetStarted
and a question card with a but ton linked to
RandomQuestion
. The code follows:
Sub GetStarted()
Ini 
tialize
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Ini 
tial 
ize()
Ran 
domize
End Sub
Sub RandomQuestion()
Dim first As In 
teger
Dim sec 
ond As In 
teger
first = Int(10 * Rnd)
sec 
ond = Int(10 * Rnd)
an 
swer = InputBox("What is " & first & " + " & sec 
ond & "?")
If an 
swer = first + sec 
ond Then
DoingWell
Else
DoingPoorly
End If
End Sub
Sub DoingWell()
MsgBox ("Good job")
End Sub
Sub DoingPoorly()
MsgBox ("Try to do better")
End Sub
GetStarted
is the same as our usual
GetStarted
al though in this ex ample we
don’t use the student’s name so we don’t call
YourName
. You could add the
Dim
userName
, the 
YourName
pro ce dure, and ap pro pri ate ref er ences to
userName
144 More Tricks for Your Scripting Bag


in
DoingWell
and 
DoingPoorly
if you want. Be cause we are not keeping
track of any thing,
Ini 
tial 
ize
just shuffles the deck by calling
Ran 
dom 
ize
.
The heart of the pro cedure is
RandomQuestion
. This generates two ran -
dom numbers from 0 to 9 and stores them in the variables
first
and 
sec 
ond
. If
you want them to be something other than from 0 to 9, use the earlier for mula to
figure it out. Next, it puts up an 
InputBox
ask ing for the stu dent to type the sum
of those two numbers. Then, it checks to see whether the an swer was right by
comparing what was typed to 
first + sec ond
, which is the right answer. You
can change this to multiplication or subtraction by us ing * or – in stead of +. You
can add a 
third
vari able to make this into a problem with three num bers. You
can even display the prob lem in a text box by us ing some of the tools for manipu-
lating text from Chap ter 6. You might have an easier time for matting the num -
bers into columns if you use a text box or more than one text box.
Keep ing Score
With some minor mod ifications, we can plug 
RandomQuestion
into some
of our other quiz zes from Chap ter 7. We’ll start by keeping score. Start with the
code from “Keeping Score” in Chap ter 7 (see Fig ure 7.1, page 93). Add the fol -
low ing 
RandomQuestion
pro 
cedure (this is the same as the previous

Download 1.37 Mb.

Do'stlaringiz bilan baham:
1   ...   146   147   148   149   150   151   152   153   ...   191




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling