Guess the number Game java

 


    import java.util.Scanner;

    class Game{
    int guessCount =0;
    int userGuess;
    public boolean win=true;
    int computerGuess;
        public Game() {
            int cg = (int ) (1+(Math.random()*10));
            computerGuess = cg;
            // System.out.println(computerGuess);
            System.out.println("Guess The no between 1 - 10");
        }

    // creating methode for setting user input
        public void takeUserInput(int i){
            int ug = i;
            userGuess = ug;
            / / System.out.println(userGuess);
          }

    // creating methode for cheaking logic

        public int isCorrect(){
            if (userGuess == computerGuess) {
            return 0;
               }
               else {
                    if (userGuess > computerGuess) {
                    return 1;
                 }
                  else {
                    if (userGuess < computerGuess) {
                    return 3;
                   }
        }
        return -1;
    }
    }
    public class GuessTheNO {
    public static void main(String[] args) {


Game playOne = new Game();

while (true){
Scanner sc = new Scanner(System.in);
int userInput = sc.nextInt();
playOne.takeUserInput(userInput);
playOne.isCorrect();
if (playOne.isCorrect()==0){
playOne.guessCount++;
System.out.println("eYou win Your Score : "+ playOne.guessCount);
System.out.println("New game Enter No Between 1 - 10");
playOne.guessCount =0;

}
else {
if (playOne.isCorrect()==1){
playOne.guessCount++;
System.out.println("Try Again ! Enter smaller No than ..."+playOne.userGuess);
}
else {
if (playOne.isCorrect()==3){
playOne.guessCount++;
System.out.println("Try Again ! Enter large No than ..."+playOne.userGuess);
}
}
}
}

}
}

Ranjit Patil

Ranjit is a passionate engineer specializing in AI/ML, Java, and Spring Boot. He loves talking about technology and is endlessly curious about the creation of the universe.

Post a Comment

comment here

Previous Post Next Post