Guess What I’m Thinking? Random Number Generator.

12.01.2018 |

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

 

Welcome to the sixth of ten lessons about the Java programming language. I hope you are still enthused from the last lesson. Ready to jump in? Today, you are going to learn how to write code that generates a random number and uses it in a number guessing game.

 

Step 1: Preparation

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

• Call today’s project, Lesson 6.

• Create a class file called, MindReader.

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

Now you are ready to start coding.

 

Step 2: Start Coding

• To generate a random number, you must ask for help from the Random number generator that is part of the Java platform itself. To do this, you need to import the Random class using the import statement, just as you did with the Scanner class. It looks like this: import java.util.Random;. It should be added above the class MindReader { line of code.

• To generate a random number, you need to create an instance of the Random class like this: Random rand = new Random();.

• Then to generate a random number, call the .nextInt(3); method and pass it the maximum number in the range of random numbers it can choose. In this case, the maximum number is 3. The random numbers it will generate are between 0 and 3.

• The random number chosen must be stored in a variable and will look like this: int number = rand.nextInt(3);. Now the random number is stored in the variable number.

• The next part of the coding you should be able to do by yourself, as it uses code you have already learned. You need to write code that:

○ creates an infinite loop

○ asks the user for a guess

○ uses an if statement to test if the guess is equal to the random number

▪ If the guess is correct, print a Congratulations message to the screen and exit the loop.

▪ Otherwise, print, “Wrong!! Try again.

A few tips:

○ To break out of the loop, use the keyword

○ To convert a number to a String, use this code: Integer.toString(number).

• Try to do this yourself without looking at the code below.

 

Step 3: Check and Run Your Program

Here is the code in full:

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

     Scanner scanner = new Scanner(System.in);
     Random rand = new Random();
     int number = rand.nextInt(3);

     while(true){

      System.out.print(“Enter your guess: “);
      String guess = scanner.next();

      if(guess.equals(Integer.toString(number))){
        System.out.println(“Congratulations!! You guessed right”);
        break;
      } else {
        System.out.println(“Wrong!! Try again”);
      }

     }
   }
}

Now run your program by clicking the Run button in the top right-hand corner of the screen. Try to guess the random number chosen. To make it more difficult, increase the maximum random number that can be chosen.

Congratulations, you have successfully completed your sixth Java program!

In Lesson 7, you will learn how to write code that combines using loops and random numbers to create a lotto number generator. So, see you then.

 

Recommended book

Java: The Complete Reference by Herbert Schildt

 

Share with friends