QBASIC
Number Guesser
In this game, the computer picks a number between 1 and 10. You have to try and guess the number.
In this game, the computer picks a number between 1 and 10. You have to try and guess the number.
CLS
RANDOMIZE TIMER
PRINT "Welcome to Number Guesser"
PRINT "I'm thinking of a number between 1 and 10."
Number = INT(RND * 10 + 1)
DO
INPUT "What is your guess? ", Guess
IF Guess = Number THEN
PRINT "Correct!"
EXIT DO
ELSE
PRINT "Try again"
END IF
LOOP
END
RANDOMIZE TIMER
PRINT "Welcome to Number Guesser"
PRINT "I'm thinking of a number between 1 and 10."
Number = INT(RND * 10 + 1)
DO
INPUT "What is your guess? ", Guess
IF Guess = Number THEN
PRINT "Correct!"
EXIT DO
ELSE
PRINT "Try again"
END IF
LOOP
END
DATA STRUCTURE
Single-Precision:
The single-precision type handles numbers with decimals. You can go up to seven digits with a single-precision variable. In a DIM statement, use "SINGLE" to create a single-precision variable. The type-character for a single-precision variable is "!". Unless you do something special, any variable without a type character is single-precision. Here are some examples of creating and using single-precision variables: X = 1.5
DIM Y AS SINGLE
Y = 2.1
Z! = 2.5
PRINT X; Y; Z!
Notice that the DIM statement can be used to tell QBASIC the type of a variable. Then you don't need to use a type character for that variable.
The single-precision type handles numbers with decimals. You can go up to seven digits with a single-precision variable. In a DIM statement, use "SINGLE" to create a single-precision variable. The type-character for a single-precision variable is "!". Unless you do something special, any variable without a type character is single-precision. Here are some examples of creating and using single-precision variables: X = 1.5
DIM Y AS SINGLE
Y = 2.1
Z! = 2.5
PRINT X; Y; Z!
Notice that the DIM statement can be used to tell QBASIC the type of a variable. Then you don't need to use a type character for that variable.
String:
The string type handles strings of characters. You cannot do math with string variables. In a DIM statement, use "STRING" to create a string variable. The type-character for a string variable is "$". Here are some examples of creating and using string variables: X$ = "Hello"
DIM Y AS STRING
Y = "Goodbye"
PRINT X$
PRINT Y
The string type handles strings of characters. You cannot do math with string variables. In a DIM statement, use "STRING" to create a string variable. The type-character for a string variable is "$". Here are some examples of creating and using string variables: X$ = "Hello"
DIM Y AS STRING
Y = "Goodbye"
PRINT X$
PRINT Y
Integer:
The integer type handles numbers without decimals. Integers may range from -32768 to 32767. Math with integers may be faster than math with single-precision variables. For programs that have to run very fast, using integers might be useful. In a DIM statement, use "INTEGER" to create an integer variable. The type-character for an integer variable is "%". Here are some examples of creating and using integer variables: X% = 32
DIM Y AS INTEGER
Y = 55
PRINT X%; Y
Since math with integers is very fast, you will commonly see the following line near the beginning of QBASIC programs: DEFINT A-Z
This tells QBASIC to stop assuming that every variable is single-precision, and instead to assume that all variables are integers. This way you don't need to use DIM or the "%" symbol throughout your program to make all your variables integers.
The integer type handles numbers without decimals. Integers may range from -32768 to 32767. Math with integers may be faster than math with single-precision variables. For programs that have to run very fast, using integers might be useful. In a DIM statement, use "INTEGER" to create an integer variable. The type-character for an integer variable is "%". Here are some examples of creating and using integer variables: X% = 32
DIM Y AS INTEGER
Y = 55
PRINT X%; Y
Since math with integers is very fast, you will commonly see the following line near the beginning of QBASIC programs: DEFINT A-Z
This tells QBASIC to stop assuming that every variable is single-precision, and instead to assume that all variables are integers. This way you don't need to use DIM or the "%" symbol throughout your program to make all your variables integers.
Long-Integer:
The long-integer type handles numbers without decimals. Long-integers may range from -2147483648 to 2147483647. Math with long-integers is usually almost as fast as math with integers. For programs that have to run very fast, using long-integers might be useful. In a DIM statement, use "LONG" to create a long-integer variable. The type-character for a long-integer variable is "&". Here are some examples of creating and using long-integer variables: X& = 65536
DIM Y AS LONG
Y = 121072
PRINT X&; Y
The long-integer type handles numbers without decimals. Long-integers may range from -2147483648 to 2147483647. Math with long-integers is usually almost as fast as math with integers. For programs that have to run very fast, using long-integers might be useful. In a DIM statement, use "LONG" to create a long-integer variable. The type-character for a long-integer variable is "&". Here are some examples of creating and using long-integer variables: X& = 65536
DIM Y AS LONG
Y = 121072
PRINT X&; Y
Double-Precision:
The double-precision type handles numbers with decimals. You can go up to fifteen digits with a double-precision variable. Double-precision variables are used where very accurate math is needed. In a DIM statement, use "DOUBLE" to create a double-precision variable. The type-character for a double-precision variable is "#". Here are some examples of creating and using double-precision variables: X# = 3.14159265358979
DIM Y AS DOUBLE
Y = 1.23456789012345
PRINT X#; Y
The double-precision type handles numbers with decimals. You can go up to fifteen digits with a double-precision variable. Double-precision variables are used where very accurate math is needed. In a DIM statement, use "DOUBLE" to create a double-precision variable. The type-character for a double-precision variable is "#". Here are some examples of creating and using double-precision variables: X# = 3.14159265358979
DIM Y AS DOUBLE
Y = 1.23456789012345
PRINT X#; Y
SELECT CASE
IF...THEN...ELSE is fine if you only have two things you want to check. What if you have 5 or 6 friends that might use your computer and you want the computer to say something different to each of them? Try this:
CLS
INPUT "Enter your name: ", Name$
SELECT CASE Name$
CASE "Ted"
PRINT "Greetings, oh powerful master"
CASE "Mike"
PRINT "Go away!"
CASE ELSE
PRINT "Hello, "; Name$; ". How are you?"
END SELECT
Whew, that was a big one. Fortunately we learned how to save in Chapter 8. Save it if you want before running it. Feel free to change "Ted" and "Mike" to "Laura" and "Robin" or whoever.
INPUT "Enter your name: ", Name$
SELECT CASE Name$
CASE "Ted"
PRINT "Greetings, oh powerful master"
CASE "Mike"
PRINT "Go away!"
CASE ELSE
PRINT "Hello, "; Name$; ". How are you?"
END SELECT
Whew, that was a big one. Fortunately we learned how to save in Chapter 8. Save it if you want before running it. Feel free to change "Ted" and "Mike" to "Laura" and "Robin" or whoever.
SELECT CASE can also be used with numbers as well as strings. Here's a quick example:
CLS
INPUT "Enter a number: ", Number
SELECT CASE Number
CASE 1234
PRINT "Thank you for entering the secret number 1234"
CASE 22
PRINT "Well, 22 is an interesting number"
CASE ELSE
PRINT "You must not know the secret number"
END SELECT
INPUT "Enter a number: ", Number
SELECT CASE Number
CASE 1234
PRINT "Thank you for entering the secret number 1234"
CASE 22
PRINT "Well, 22 is an interesting number"
CASE ELSE
PRINT "You must not know the secret number"
END SELECT

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home