# Lotto Number Generator Ka-Ching! Combine Loops and Random Numbers.

12.01.2018

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

Welcome to the seventh of ten lessons about the Java programming language. I hope you are still enthused from the last lesson. Let’s jump straight in. Today, you are going to learn how to write code that combines a loop and random number generation to create a lotto number.

Step 1: Preparation

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

• Call today’s project, Lesson 7.

• Create a class file called, LottoNumberGenerator.

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

Now you are ready to start coding.

Step 2: Start Coding

The lotto number generator will ask for the number of lotto balls and the maximum number that a lotto ball can have. You will use the Scanner class for this task.

Then it will loop for as many times as there are balls, and choose a random number for each loop. The collection of numbers it generates will form the final lotto number. You will use the Random class and create a while loop for this task.

I am not going to hold your hand in this lesson. Now is your chance to show what you have learned.

The lotto program needs:

• a loop counter to count the number of times the loop has run

• to convert the response to the questions into the correct data format, which is integer; use this code for that task: new Integer(balls)

You have already written code that does all the things your lotto program must do. You just have to put it all together. Here are a few tips that might help you with this task:

• You only need to create one Scanner instance. It can be reused.

• You can print out the numbers as they are chosen, so you don’t have to store them and print then out afterward.

Have a go at this task and see how you do. Try to resist the temptation to peek at the solution below.

Step 3: Check and Run Your Program

Here is the code in full:

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

Scanner scanner = new Scanner(System.in);
System.out.print(“Number of balls “);
String balls = scanner.next();

System.out.print(“Max single number “);
String max = scanner.next();
Random rand = new Random();

int loopCounter = 0;
while(loopCounter < new Integer(balls)){
int x = rand.nextInt(new Integer(max));
System.out.println(x);
loopCounter++;
}

}
}

You can now run your program by clicking the Run button in the top right-hand corner of the screen. Enter the number of balls and then the maximum number, and see the randomly generated lotto number printed to the console.

Congratulations, you have successfully completed your seventh Java program!

In Lesson 8, you will learn how to write code that stores a collection of data in an array. So, see you then.

Recommended book

Java: An Introduction to Problem Solving and Programming by Walter Savitch

Share with friends