19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 6-2: The text window waiting for input from<br />

your user<br />

When <strong>Small</strong> <strong>Basic</strong> runs the statement radius = TextWindow.ReadNumber(),<br />

it waits for the user <strong>to</strong> enter a number and press enter. The program won’t<br />

read what the user types until they press enter. When the user presses<br />

enter, the program grabs the user input and assigns it <strong>to</strong> the radius variable.<br />

The program then continues <strong>with</strong> the statement after the ReadNumber()<br />

method.<br />

Now that you’ve accepted the radius from your user, all that’s left is <strong>to</strong><br />

compute the area and display the result. Listing 6-2 shows the complete<br />

program.<br />

1 ' CircleArea3.sb<br />

2 TextWindow.Write("Please enter a radius; then press : ")<br />

3 radius = TextWindow.ReadNumber()<br />

4<br />

5 area = 3.1416 * radius * radius<br />

6 TextWindow.WriteLine("Area = " + area)<br />

Listing 6-2: Letting the user enter the radius<br />

Let’s see what the output looks like if you enter a radius of 8:<br />

Please enter a radius; then press : 8<br />

Area = 201.0624<br />

Try it out for yourself!<br />

A Moment of Silence, Please (Pause)<br />

At times you may need <strong>to</strong> display some instructions <strong>to</strong> your user (like explaining<br />

the rules of Hide and Go Tickle) and then wait for them <strong>to</strong> read those<br />

instructions. For example, you might display the instructions followed by<br />

“Press any key <strong>to</strong> continue . . . ” and then wait for the user <strong>to</strong> press a key<br />

<strong>to</strong> show that they’re ready <strong>to</strong> continue. You can do this by using the Pause()<br />

method.<br />

To see this method in action, let’s write a program that converts the computer<br />

in<strong>to</strong> a wisdom machine. Each time the user presses a key, the computer<br />

displays a new bit of wisdom. The program is shown in Listing 6-3.<br />

1 ' WisdomMachine.sb<br />

2 TextWindow.WriteLine("WISDOM OF THE DAY")<br />

3<br />

4 TextWindow.WriteLine("A friend in need is a friend indeed.")<br />

76 Chapter 6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!