# Lucky Dip: Using Arrays and Random Numbers

12.01.2018

Episode #9 of the course An introduction to Java programming by Alex Theedom

Welcome to the ninth of ten lessons about the Java programming language. Congratulations on making it this far! You’re almost finished, so let’s get to it. Today, you are going to learn how to randomly select an element for an array.

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 9.

• Create a class file called, LuckDip.

• Delete the line, System.out.println(“Hello Codiva”);.

Now you are ready to start coding.

Step 2: Start Coding

In a previous lessons, you wrote code that generated a random number and code that stored and retrieved data from an array. In this lesson, you will combine what you learned in those lessons and code a lucky dip game.

Now, as you already know the code necessary to develop this game, I am only going to talk through the game’s requirements, and you are going to be responsible for programming it—just as if you were working at a company, programming a game for a client. The game’s requirements are:

• A String array of length 10 to hold the prizes. Some of the elements in the array will be “No Prize,” so at least sometimes you don’t win a prize.

• A random number generator that generates an integer between 0 and 9.

• A Scanner that asks the game player to try again or quit.

• A infinite while loop in which a random integer is selected and an element from the array is chosen.

• After the element has been chosen, the prize is printed to the console.

• The game player is asked to go again or quit.

Now you know what is required, try to program the game without peeking at the solution below.

Step 3: Check and Run Your Program

Here is the code in full:

import java.util.Scanner;
import java.util.Random;
class LuckyDip {
public static void main(String[] args) {

String[] prizes = new String[10];
prizes[0] = “Small Whistle”;
prizes[1] = “No Prize”;
prizes[2] = “Colored Pen”;
prizes[3] = “Notebook”;
prizes[4] = “No Prize”;
prizes[5] = “Plastic Doll”;
prizes[6] = “Bubble Gum”;
prizes[7] = “No Prize”;
prizes[8] = “Toy Car”;
prizes[9] = “No Prize”;

Random random = new Random();
Scanner scanner = new Scanner(System.in);

while(true){
System.out.println(“Your luck dip prize is: “ + prizes[random.nextInt(9)]);
System.out.println(“Go again? (y/n) “);
}

}
}

Your solution might be slightly different from mine. That doesn’t matter, as long as it conforms to the requirements for the game and works as expected.

Notice the code line: if(answer.equals(“n”)) break;. I have missed out the braces. This is a shortcut way to write a simple if statement.

You can now run your program by clicking the Run button in the top right-hand corner of the screen. Good luck. I hope you win.

Congratulations, you have successfully completed your ninth Java program!

In Lesson 10, the last lesson, you will put together everything you have learned to build a hangman game. So, see you then.

Recommended book

Java in a Nutshell: A Desktop Quick Reference by Benjamin J Evans,‎ David Flanagan

Share with friends