Error in Guess the Number Program in Java in which the Program Generates and takes an User Input to check the guessed number is correct or not

I made a java program named “Guess the number” in which the computer will generate a random number and we will have to give a number in the input. The computer will check it and tell us the number we guessed by giving in the input.I created a java program named “Guess the number” in which the computer will generate a random number and we have to give a number as input. The computer will check and tell whether the number we guessed by giving it in the input is correct and matches the generated number or not.
If our guessed number is more than the generated number then If our guessed number is more than the generated number then we will see “Too High…” in the output and if it is smaller then “Too Low…” will be shown.

I am getting an error in the program I made that whatever number I give in the input, it always appears as “Too High…”. I also looked at its solution and my code was not correct.I am getting an error in the program I made that whatever number I give in the input it always comes out as “Too High…”. I also looked at its solution, my code is correct, only I have not called the method which says No Of Guess or Attempts.

My Code:






import java.util.Random;



import java.util.Scanner;



class Game{



    public int number;



    public int inputNumber;



    public int noOfGuess = 0;



    



    public void setNoOfGuess(int noOfGuess){



        this.noOfGuess = noOfGuess;



    }



    public int getNoOfGuess(){



        return noOfGuess;



    }



    



    Game(){



        Random rand = new Random(100);



        this.number = rand.nextInt();



    }



    void takeUserInput(){



        System.out.println("Guess the Number:");



        Scanner sc = new Scanner(System.in);



        inputNumber = sc.nextInt();



    }



    boolean isCorrectNumber(){



        if(inputNumber==number){



            System.out.println("You've Guessed the Correct Number");



            return true;



        }



        else if(inputNumber<number){



            System.out.println("Too Low...");



        }



        else if(inputNumber>number){



            System.out.println("Too High...");



        }



        return false;



    }



}



public class Main



{



    public static void main(String[] args) {



        Game g = new Game();



        boolean b = false;



        while(!b){



        g.takeUserInput();



        b = g.isCorrectNumber();



        }



    }



}

Solution:






package com.company;



import java.util.Random;



import java.util.Scanner;







class Game{



    public int number;



    public int inputNumber;



    public int noOfGuesses = 0;







    public int getNoOfGuesses() {



        return noOfGuesses;



    }







    public void setNoOfGuesses(int noOfGuesses) {



        this.noOfGuesses = noOfGuesses;



    }







    Game(){



        Random rand = new Random();



        this.number = rand.nextInt(100);



    }



    void takeUserInput(){



        System.out.println("Guess the number");



        Scanner sc = new Scanner(System.in);



        inputNumber = sc.nextInt();



    }



    boolean isCorrectNumber(){



        noOfGuesses++;



        if (inputNumber==number){



            System.out.format("Yes you guessed it right, it was %dnYou guessed it in %d attempts", number, noOfGuesses);



            return true;



        }



        else if(inputNumber<number){



            System.out.println("Too low...");



        }



        else if(inputNumber>number){



            System.out.println("Too high...");



        }



        return false;



    }







}



public class Main{



    public static void main(String[] args) {



        /*



            Create a class Game, which allows a user to play "Guess the Number"



            game once. Game should have the following methods:



            1. Constructor to generate the random number



            2. takeUserInput() to take a user input of number



            3. isCorrectNumber() to detect whether the number entered by the user is true



            4. getter and setter for noOfGuesses



            Use properties such as noOfGuesses(int), etc to get this task done!



         */







        Game g = new Game();



        boolean b= false;



        while(!b){



        g.takeUserInput();



        b = g.isCorrectNumber();



        }







    }



}





New contributor

Ankur Jha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật