Wednesday, January 2, 2008

Console Input

 

In all of our examples so far, we have had to modify the program each time to change the values of a variable. Many programs though require input from the user. This allows us to ask the user of our programs questions and then have our computer program do something based on the input of the user.

In this section, we will program a guessing game where the computer this of a number between 1 and 100 and the user has to try and guess the number. Each time the user guesses, the computer will tell the user higher or lower. The game stops when the user guesses the number correctly. At the end, the computer will tell the user how many times they guessed before getting the answer write.

To read input from the user, we need to use one of the following two methods

ConsoleReadInt()

ConsoleReadLine()

ConsoleReadInt() is used to input integers from the user. ConsoleReadLine() is used to read text from the user. In this example, you are writing guessing game. Because the input are integers, we will use ConsoleReadInt() to get input from the user.

When you ask someone a question, you get an answer. Where do you store the answer? In your memory. The same is true when you use ConsoleReadInt(). The computer asks the user a question and the users answer is stored in the computers memory which we define as a variable.

Before we can use the variable it must be defined. What type of variable should we define? Integer, right?

Let’s take a quick look at an example:

Program MyNewProgram

Method Main()

Define X As Integer

X = ConsoleReadInt("Enter a number: " , False)

ConsoleWriteLine ("You entered: " + X)

End Method

End Program

In this example, we can see that ConsoleReadInt() takes two paramters.

The first Parameter Prompt determines what message is displayed to the user. The second parameter EchoToConsole determines if the value the user enters is displayed in the console window. Typically the value of EchoToConsole should be set to false.

clip_image002[4]

Let’s review the ConsoleReadInt() Method

clip_image004

This program asks the user to input a variable and then write the value of the variable on the screen. Pretty simple.

How do we determine what number the computer is thinking? Since we want the number to be different each time the user plays the game we can have the computer generate a random number using the Random() method. Random() asks the computer to pick a number between two numbers. This is just like when someone asks you to pick a number between 1 and 10.

To tell the computer to pick a number between 1 and 10 we use Random(1,10).

clip_image006[4]

Again, just like with ConsoleReadInt() we need to store the value returned by the method Random(). What type of variable should we use? Let’s define a variable called Answer as an Interger and return a number between 1 and 100.

Define Answer As Integer

Answer = Random (1,100)

Now let’s build program that puts all of the pieces together. We want to

1. Have the computer pick a number between 1 and 100 and store it in the variable answer

2. Have the user guess a number between 1 and 100

3. Repeat this process until the users guess is equal to the anwer

In the next section, we will add logic to the code to tell the computer if the number is higher or lower.

First we need to define our variable. This program will start with 2 variables. One for the Answer and one for Guess.

Define Answer As Integer

Define Guess As Integer

Next, we need the computer to select a number between 1 and 100

Answer = Random (1,100)

Now we need to setup a Loop. Since we want the program to repeat until the user gets the answer correct, we will use a While Loop. The computer will keep asking the user while their guess is NOT equal to their answer.

While Answer <> Guess

Then we will prompt the user to enter a number and display the number the user entered on the console window.

Guess = ConsoleReadInt("Enter a number between 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

The program will continue until the user guess the right answer and then we can display a message telling the user they got the answer right.

End While

ConsoleWriteLine ("You guessed right.")

Next, let’s prompt the user to enter a number between 1 and 100.

X = ConsoleReadInt("Enter a number between 1 and 100: " , False)

Here is what our program should look like.

Program GuessANumber

Method Main()

Define Answer As Integer

Define Guess As Integer

Answer = Random (1,100)

While Answer <> Guess

Guess = ConsoleReadInt("Enter a number 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

End While

ConsoleWriteLine ("You guessed right.")

End Method

End Program

Now, have some fun and see if you can guess the number the computer is thinking.

Right now, the user has to keep guess until they get the number right. Typically when you play this game, the person you are playing with will say Higher or Lower based on the number you say. Let’s look at how we can modify the computer program to do the same. Here is what we want to do.

If the guess the user enters is higher than the answer then display Lower

If the guess the user enters is lower than the answer then displayer Higher

This can be performed by using an If / Then statement. Basically you are saying, if something is true then do this. Let’s take a look at what this looks like.

If Guess > Answer Then

ConsoleWriteLine ("The number is LOWER")

End If

Here we are telling the computer if the value of the variable Guess is greater than the value of the variable Answer then display a message “The number if Lower”. Now we need to do the same and tell the user if the number is higher.

If Guess < Answer Then

ConsoleWriteLine ("The number is HIGHER")

End If

Now our complete program looks like this.

Program GuessANumber

Method Main()

Define Answer As Integer

Define Guess As Integer

Answer = Random (1,100)

While Answer <> Guess

Guess = ConsoleReadInt("Enter a number between 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

If Guess > Answer Then

ConsoleWriteLine ("The number is LOWER")

End If

If Guess < Answer Then

ConsoleWriteLine ("The number is HIGHER")

End If

End While

ConsoleWriteLine ("You guessed right.")

End Method

End Program

In this section we are going to create a program to write a math quiz for students. The program will ask the user 10 questions and allow the user to enter the answer for each question. At the end of the quiz the program will display the number of questions right and the number of questions wrong.

No comments: