19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5 TextWindow.Pause()<br />

6<br />

7 TextWindow.WriteLine("A hungry man is an angry man.")<br />

8 TextWindow.Pause()<br />

9<br />

10 TextWindow.WriteLine("Love your enemies. They hate that.")<br />

Listing 6-3: Demonstrating the Pause() method<br />

After showing the first wise saying (line 4), the program calls Pause()<br />

<strong>to</strong> give the user time <strong>to</strong> read it (line 5). This call displays “Press any key <strong>to</strong><br />

continue . . .” and waits for the user <strong>to</strong> press a key. When the user presses<br />

a key, the program displays the next bit of wisdom (line 7) and pauses<br />

again (line 8). The program continues <strong>to</strong> do this until the last statement<br />

is executed. Add more wise sayings <strong>to</strong> this program, and then share it <strong>with</strong><br />

someone!<br />

What if you want <strong>to</strong> display a statement other than “Press any key <strong>to</strong><br />

continue . . . ,” such as “Press any key <strong>to</strong> see the next line of wisdom . . .”?<br />

Well, <strong>Small</strong> <strong>Basic</strong> provides you <strong>with</strong> the PauseWithoutMessage() method for this<br />

purpose. You can write a cus<strong>to</strong>m prompt using Write() or WriteLine() as usual,<br />

and then call PauseWithoutMessage() <strong>to</strong> wait for the user. Try it out. Replace the<br />

calls <strong>to</strong> Pause() in lines 5 and 8 of Listing 6-3 <strong>with</strong> these statements:<br />

TextWindow.WriteLine("Press any key <strong>to</strong> see the next line of wisdom...")<br />

TextWindow.PauseWithoutMessage()<br />

Your program runs the same way as before but uses a more descriptive<br />

prompt.<br />

Working <strong>with</strong> User Input<br />

Let’s put your new knowledge <strong>to</strong> use by writing a couple of programs that<br />

read input from a user, process the input, and display the output back <strong>to</strong><br />

the user.<br />

Converting Fahrenheit <strong>to</strong> Celsius<br />

Next, you’ll create a program that converts a temperature from degrees<br />

Fahrenheit <strong>to</strong> degrees Celsius. The program prompts the user for the temperature<br />

in Fahrenheit and then converts it <strong>to</strong> Celsius using this formula:<br />

C = ( 5÷ 9) ×( F − 32)<br />

Run the program in Listing 6-4 several times <strong>to</strong> see how it works. To use<br />

the degree symbol, press and hold the alt key, type 248 on your numeric<br />

keypad, and then release the alt key.<br />

1 ' Fahrenheit2Celsius.sb<br />

2 TextWindow.Write("Enter a temperature in °F: ")<br />

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

Getting User Input 77

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

Saved successfully!

Ooh no, something went wrong!