CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Guess the number Game in JAVA

JAVA November 19, 2025 4 views 0 likes
Write a program in JAVA to guess the number.

ANSWER

numGuess - Java Code

numGuess - Java Code


import java.util.*;
public class numGuess
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("i'm thinking a number Can you Guess it");
        int gus = (int)((Math.random() * 100) + 1);
        int rnd = -1;
        int attm = 1;
        
        while (gus != rnd)
        {
            System.out.println("Enter the number: ");
            rnd = sc.nextInt();
            
            if (rnd > gus) {
                System.out.println("Too High");
                attm = attm + 1;
            }
            else if (rnd < gus) {
                System.out.println("Too Low");
                attm = attm + 1;
            }
            else {
                System.out.println(gus + " is equal to " + rnd);
                break;
            }
        }
        
        System.out.println("Congratulation You guess the number only " + attm + " attems");
    }
}

OUTPUT:

Number Guessing Game

Number Guessing Game

I'm thinking of a number between 1 and 100. Can you guess it?

Result:

Attempts:

4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285