Number Guessing Game in Java Tutorial (Beginning to End)




//In this video I will show you how we create a simple game using Java Console

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

public class GuessTheNumber {
  public static void main(String args[]) {
   Scanner scanner = new Scanner(System.in);
   Random random = new Random();
   
   int randomNumner = random.nextInt(100)+1;
   int attempts = 0;
   boolean hasGuessed = false;
   
   System.out.println("Wellcome to the Guess the Numner Game");
   System.out.println("i am thinking of a number between 1 and 100");
    
    while(!hasGuessed){
        
    System.out.print("Enter your Guess");
    int guess = scanner.nextInt();
    attempts++;
    
    if(guess<randomNumner){
        
        System.out.println("Too low");
    }else if(guess>randomNumner){
        
        System.out.println("to large");
    }else{
        
        
        System.out.println("Congurtulations  you guessed the numenr in  "+attempts+" attempts" );
        hasGuessed =true;
    }
        
    }
    scanner.close();

   
  }
}

Comments

Popular posts from this blog

JavaFX UI /UX Dashboard

JavaFX code for responsive layout