# Cd

Figures 2-2 through 2-5 illustrate the game play of Guess the Password. Figure 2-2 Guessing the secret password.

Want to pause the game or create random numbers to make artificial intelligence simpler? Just go on to the next project.

Compiling 1 souEce file to C;\Users\Ian Ci compile:

run:

rOU COT THE SECP.ET MESSAGE!!!

BUILD SUCCESSFUL (total time; 38 seconds)

Finished building S192 (run).

Figure 2-2 Guessing the secret password.

Figure 2-3 Correct guess! Figure 2-4 Guessing a different password.

mic: deps-jar:

Compiling 1 source file to C: \TJsers\Ian C

compile:

run:

BUILD SUCCESSFUL (total time: 11 seconds)

Finished building S1P2 (run).

Figure 2-5 Incorrect guess.

Project 3: Number Cruncher

### Project

The program displays a math equation. The player must solve the problem before the computer counts down from three and displays the correct answer. Can you beat the computer?

New building blocks

Random numbers, Pausing

Random numbers

All random numbers are of type double, because they are between 0 (inclusive) and 1 (exclusive). There is a simple command to create random numbers.

To create a random double between the value of 0 and 1, use the following code:

Math.random()

This returns a value, similar to the way JOptionPane returns values. However, this returns a double instead of a String.

So ... if you want to create a random 1 digit number, try the following:

int rand =

(int) makes sure that it is int variable format

Math.round() rounds it to the nearest whole number

Pausing

You can make the computer pause for a given period by using a very simple command:

100 is the number of milliseconds you want to sleep (this can also be an int variable).

Also, you must add something to the last part of the main method:

After (String[] args), add "throws Exception" The entire line should look like:

public static void main (String[ ] args) throws Exception

Making the Game

OK ... you can now make a game that can be used in math competitions around the world!

Create a math equation with two numbers using any of these operations: adding, subtracting, multiplying, dividing, or modding.