# Game-Arrays01: A Guessing Game with Random Messages - Exercise

Game-Arrays01: A Guessing Game with Random Messages - Exercise

# Game-Arrays01: A Guessing Game with Random Messages

15 20

(Optional - if time permits)

1. Modify your guessing game program to hold an array of several different `String` messages, all of which have some form of message for "Correct".
2. Generate a random number in the range from 0 to the size of the array.
3. Use this value to select one message to print when they guess correctly.
4. Continue this approach for "Too Low" and "Too High".
```import util.*;
import java.util.*;

public class Game {
private static String [] correctMessages =
{ "Correct", "Right on!", "Most Excellent!" };
private static String [] lowMessages =
{ "Too low", "Try higher next time" };
private static String [] highMessages =
{ "Too high", "Try lower next time" };

public Game() {
Random r = new Random();
}

public void play() {
int guess;
Random r = new Random();
do {
guess = KeyboardReader.getPromptedInt("Enter a number 1 -100: ");
System.out.println(lowMessages[r.nextInt(lowMessages.length);
System.out.println(highMessages[r.nextInt(highMessages.length);
System.out.println(correctMessages[r.nextInt(correctMessages.length);
}

public static void main(String[] args) {
char playAgain = 'Y';
do {
new Game().play();
playAgain = KeyboardReader.getPromptedChar("Play again (y/n)?: ");
} while (playAgain == 'Y' || playAgain == 'y');
}
}
```

There are three arrays of messages; note that they are not all the same size. The code to handle too high, too low, and correct generates a random number between 0 and the appropriate array's `length`, by using the `Random` object's `nextInt` method, and passing the length of the array.