Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

guessing game in java

package com.company;

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

class Game {
    public int noOfGuess;
    public int compGuess;
    public int takeUserInput;

    Game() {
        Random random = new Random();
        this.compGuess = random.nextInt(100);
    }

    public int getNoOfGuess() {
        return noOfGuess;
    }

    public void setNoOfGuess(int noOfGuess) {
        this.noOfGuess = noOfGuess;
    }

    public void takeUserInput() {
        System.out.println("Enter your Guess");
        Scanner scanner = new Scanner(System.in);
        takeUserInput = scanner.nextInt();
    }

    public boolean checkingTheGame() {
        noOfGuess = 1 + noOfGuess;
        if (takeUserInput == compGuess) {
            System.out.println("YOU WON!!");
            return true;
        } else if (takeUserInput < compGuess) {
            System.out.println("Your guess is TOO LOW!!");
        } else {
            System.out.println("Your guess is TOO HIGH!!");
        }
        return false;
    }

}

public class NumberGuessingGameInOOP {
    public static void main(String[] args) {
        Game game = new Game();
        System.out.println("Welcome to the guessing game");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your name");
        String playerName = scanner.nextLine();
        boolean gamePlay = false;
        while (!gamePlay) {
            game.takeUserInput();
            gamePlay = game.checkingTheGame();
        }
        System.out.println(playerName + " your point " + game.getNoOfGuess());


    }
}
//Game should have the following methods:
//Constructor to generate the random number
//takeUserInput() to take a user input of number
//isCorrectNumber() to detect whether the number entered by the user is true
//getter and setter for noOfGuesses
// Clean code
Comment

PREVIOUS NEXT
Code Example
Java :: arraylist syntax in java 
Java :: set image programmatically android 
Java :: android application manifest 
Java :: java enums 
Java :: how to get the max value of an array java 
Java :: retrofit android 
Java :: public static void main vs static public void main 
Java :: No Java files found that extend CordovaActivity. [ERROR] An error occurred while running subprocess cordova. 
Java :: abs in java 
Java :: Java Read a Line of Text Using Scanner 
Java :: how to get color from resource android 
Java :: java localtime 
Java :: string to int 
Java :: find length of array java 
Java :: how to convert primitive int to Integer in java 
Java :: java char array to string 
Java :: java binart tree print 
Java :: java long to hours minutes and seconds 
Java :: make edittext not editable android 
Java :: heap in java 
Java :: java get attributes from class 
Java :: java standard exception 
Java :: java class jar determine 
Java :: java toggle button get state 
Java :: esponente in java 
Java :: java write to a file 
Java :: runnable java 
Java :: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.5.0:repackage failed: 
Java :: imageview.setbackground 
Java :: java search arraylist 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =