Random guessing game Java with code

Random guessing game java is one of the very first programs you write when you are learning Java. Many people doubt that what is the relationship between Java and JavaScript. So let me tell you that both Java and JavaScript are completely different languages. Java is used for the backend while JavaScript is mostly used in the frontend. However, you can also use JavaScript in the backend with the help of node.js.

Random guessing game Java with code

Java is a well-structured, object-oriented programming language that is suitable for novices. Because many procedures run automatically, you can master it quickly. You don’t have to go into too much detail about “how things work in there.” Java is a cross-platform programming language.

There are various reasons why Java remains a development industry leader and why it is still a language worth knowing. Despite the fact that Java’s many perks and capabilities make it a very diversified language, it is actually rather simple to learn and master for developers of all ability levels.

You can run the code by installing JDK and IntelliJ IDEA or you can simply run the code by installing JDK and writing the code in any code editor or even with Notepad.

Random guessing game Java with code

You can copy the code from where variables are declared.

package com.anurag;

import java.util.Random;
import java.util.Scanner;

public class game {
    public static void main(String[] args) {
        int i;
//        int r=0;
        int k = 6;
        int guess;
//        int ran = ((int) (Math.random()) * 100);
        Random rand = new Random();
        int ran = rand.nextInt(50);
//
//        int minGuess = 1;
//        int maxGuess = 100;
        Scanner sc = new Scanner(System.in);
        for (i = 0; i < k; i++) {
            System.out.println("enter the number between 0-50");
            guess = sc.nextInt();
            if (guess == ran) {
                System.out.println("you are right");
                break;
            } else if (guess > ran) {
                System.out.println("lower");
            } else if(guess< ran) {
                System.out.println("Higher");
            }else
                System.out.println("You are out of limit");

        }
    }

Hope you enjoyed the article about the random guessing game Java with code and try the code. You can modify the code to run some experiments.