Java simple projects – Number guessing game:

               Games are always interesting. let a user guess a number randomly and check the computer generated number is equal or not. The program is given below.

Class : GameNumberGuessing

Member functions: startIt(),getUserGuessedNo(), getNoByComputer()

Where startIt()- It is the beginning function to start the process.

             getUserGuessedNo() – The user interaction to get the number to be guessed

             getNoByComputer()  - It makes the computer to generate a random number.

Logic:

  • ·       A number is given by user as a guess.
  • ·       Let the computer generate a random number.
  • ·       Check the user no is equal to computer no. if both are matched, just display the number found message.
  • ·       Otherwise, repeat the process until the user guesses the correct number.

Program:

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

public class GameNumberGuessing {

  private static final int MIN_No = 1;

  private static final int MAX_No = 1000;

  public static void main(String[] args) {

    GameNumberGuessing game1 = new GameNumberGuessing();

    game1.startIt();

  }

  public void startIt() {

    boolean isUserGuessOption = false;

    int noOfGuess = 0;

    // Get the computer generated number

    int computerNo = getNoByComputer();

    // Program ends with user guesses the number correctly

    while (!isUserGuessOption) {

      int userNo = getUserGuessedNo();

      if (userNo > computerNo) {

        System.out.println("Sorry, the number you guessed is too high");

      } else if (userNo < computerNo) {

        System.out.println("Sorry, the number you guessed is too low");

      } else if (userNo == computerNo) {

        System.out.println("Congratulations! Your guess is correct!");

        isUserGuessOption = true;

      }

      noOfGuess++;

    }

    System.out.println("You found the number in " + noOfGuess + " guesses");

  }

  public int getNoByComputer() {

    return ThreadLocalRandom.current().nextInt(MIN_No, MAX_No + 1);

  }

  public int getUserGuessedNo() {

    Scanner sn = new Scanner(System.in);

    System.out.println("Please guess the number: ");

    return sn.nextInt();

  }

}

Output:

C:\raji\blog>javac GameNumberGuessing.java

C:\raji\blog>java GameNumberGuessing

Please guess the number:

234

Sorry, Please guess the higher number

Please guess the number:

345

Sorry, Please guess the higher number

Please guess the number:

456

Sorry, Please guess the higher number

Please guess the number:

678

Sorry, Please guess the higher number

Please guess the number:

1000

Sorry, Please guess the lower number

Please guess the number:

789

Sorry, Please guess the higher number

Please guess the number:

890

Sorry, Please guess the higher number

Please guess the number:

923

Sorry, Please guess the lower number

Please guess the number:

910

Sorry, Please guess the lower number

Please guess the number:

901

Sorry, Please guess the lower number

Please guess the number:

892

Congratulations! you have found the number

You found the number in 11 guesses.

That is the way of creating a number guessing program in java.

No comments:

Post a Comment